BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

While loop in Perl with example

Last Updated: September 10, 2022 by Chaitanya Singh | Filed Under: Perl

In the last tutorial, we discussed for loop in Perl. In this tutorial we will discuss while loop. As discussed in previous tutorial, loops are used to execute a set of statements repeatedly until a particular condition is satisfied.

Syntax of while loop:

while(condition)
{
   statement(s);
}

Flow of Execution of the while Loop

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute. When condition returns false, the control comes out of loop and jumps to the next statement after while loop.

Note: The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely.

Example

#!/usr/local/bin/perl

$num = 100;

while( $num > 95 ){
   printf "$num\n";
   $num--;
}

Output:

100
99
98
97
96

Top Related Articles:

  1. Perl Tutorial for beginners
  2. Until loop in Perl with example
  3. Installing Perl on Windows, Mac, Linux and Unix
  4. Perl Variables
  5. Conditional Statements in Perl

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap