Perl do while

Summary: in this tutorial, you’ll learn about Perl do while loop statement that executes a code block repeatedly as long as a condition is true.

Introduction to Perl do…while statement

The Perl do...while loop statement executes a code block repeatedly as long as a test condition is true.

Both while and do...while statements terminate the loop if the test condition is false.

Unlike the while statement that checks the condition at the beginning of each iteration, the do...while statement checks the condition at the end of each iteration.

The following illustrates the syntax of the do...while loop statement:

do { # code block } while(condition);
Code language: Perl (perl)

Because do...while loop statement checks the condition at the end of each iteration, the code block inside the loop always executes at least once.

Also, do is not a loop block. Therefore, you need to use other statements to control the loop including next, last and redo statements.

The following flowchart illustrates the do...while statement:

Perl do while loop

Perl do…while loop example

In practice, you use the do...while loop statement when you want the loop executes at least one before the condition is checked.

A typical example of this is the command-line program that requests users for input until an exit command is provided. For example:

#!/usr/bin/perl use warnings; use strict; my $command; print("Enter a command, bye to quit.\n"); do { print(">"); # convert command to lowercase chomp($command = <STDIN>); $command = lc($command); # display the command print("$command\n"); }while($command ne "bye");
Code language: PHP (php)

When you enter the by string in the command line, the condition in the do while statement becomes false that terminates the loop.

The following is the output of the program:

Enter a command, bye to quit. >perl perl >perl do while perl do while >perl do until perl do until >bye bye
Code language: JavaScript (javascript)

Perl do…while with next and last statements

The following section shows you how to use the next and last statements inside a do...while statement to control the loop.

1) Perl do…while with next statement

The next statement allows you to start the next iteration of the loop and skips the rest of the code below it.

To use the next statement inside the do...while statement, you have to define an additional loop block for the next statement as follows:

do{ # do block { statement next; } }while(condition);
Code language: Perl (perl)

or in short:

do{{ statement next; }}while(condition);
Code language: Perl (perl)

See the following example:

#!/usr/bin/perl use warnings; use strict; my @a = (1,3,2,4,6,9,8); my $sum_even = 0; my $num = 0; do {{ # get the next array element $num = shift(@a); # skip if the element is odd number next if $num % 2 == 1; # calculate total of even numbers $sum_even += $num; }}until(!scalar @a > 0); print("$sum_even\n");
Code language: Perl (perl)

How it works.

  • We have an array of integers that contains both odd and even numbers.
  • Inside the do while loop, we remove the array element by using the shift() function. We start a new iteration if the element is an odd number, otherwise, we add it up to the $sum_even variable.
  • The loop terminates only when the array @a is empty specified in the condition.

The following is the output of the program:

20

2) Perl do…while with the last statement

The last statement exits the do...while loop immediately. It acts as the break statement in C/C++.

To use do...while statement with the last statement, you need to add another block to the do...while statement like this:

loop_label:{ do{ last if expression; }while(condition) }
Code language: Perl (perl)

See the following example:

#!/usr/bin/perl use warnings; use strict; my @haystack = qw(1 3 2 4 5 9 8 6 7); my $count = scalar @haystack; my $i = 0; my $needle; print("Enter a number to search (1-9):"); $needle = int(<STDIN>); find_needle_in_haystack:{ do { if($haystack[$i] == $needle){ print("Number $needle found at position $i\n"); # exit the loop last; } # next element $i++; }until($i == $count); }
Code language: Perl (perl)

The following is the output of the program when you enter 5:

Enter a number to search (1-9):5 Number 5 found at position 4
Code language: JavaScript (javascript)

In this tutorial, you’ve learned how to use the Perl do...while statement to execute a code block repeatedly based on a condition checked at the end of each iteration.

Was this tutorial helpful ?