YouTip LogoYouTip

Perl While Loop

# Perl while Loop [![Image 4: Perl Loops](#) Perl Loops](#) The `while` statement executes a statement or group of statements repeatedly as long as a given condition is true. The condition is tested before the loop body is executed. ### Syntax The syntax is as follows: while(condition){ statement(s);} Here, `statement(s)` can be a single statement or a block of statements. `condition` can be any expression. The loop executes as long as the condition is true. When the condition becomes false, the program flow exits the loop. ### Flowchart ![Image 5: while loop in Perl](#) In the diagram, the key point of the _while_ loop is that the loop may not execute even once. If the condition is false initially, the loop body is skipped, and the program proceeds directly to the next statement following the while loop. ## Example #!/usr/bin/perl$a = 10; # Execute the while loop while($a<20){printf"a value is : $an"; $a = $a + 1; } The program executes the loop body as long as the variable `$a` is less than 20. When `$a` is greater than or equal to 20, the loop exits. Executing the above program produces the following output: a value is : 10 a value is : 11 a value is : 12 a value is : 13 a value is : 14 a value is : 15 a value is : 16 a value is : 17 a value is : 18 a value is : 19 [![Image 6: Perl Loops](#) Perl Loops](#)
← Perl For LoopPerl Unless Elsif Statement β†’