Learn about While Do-While and Until Loops in Perl

Perl (8) - Loops

Perl (8) - Loops

Welcome to part two of the Loops topic. After discussing the first two types of Loops (for and foreach), we are going to continue with the remaining types.

while Loop
The while Loop uses a condition to control the iteration process. The loop continues to execute its body as long as the specified condition is met.


Let’s re-write the factorial example using the while Loop.

Consider the following script:

Will that do the job?! Let’s see:

For the while loop to achieve the same job that the for loop does, it needs to use a similar logic:

  • A loop variable has been initialized:

  • A condition for the loop iteration is specified in the loop definition:

  • The loop variable is updated:

And also as in for loop, the while loop may not stop in either of the following cases:

  • The logic of the condition is incorrect.
  • You forget to update the loop variable.
  • The loop variable is updated in the wrong way (for example, it is incremented instead of being decremented).

Now, let’s see how the while loop doing something that the for loop couldn’t do.

We need to write a script that keeps asking the user to enter a number. This should continue as long as the user enters non-empty input. If the user enters an empty string, the loop exits. The script should print the total of the input numbers and their average.

To do so, check the following script:

Let’s see it in action.

As you see in the output, the script continues to ask the user to enter a number as long as the user enters something. When the user enters an empty string (by just pressing Enter), the loop exits, the total and average are calculated, and then printed to the user.

do…while Loop
The do…while loop is very similar to the while loop in its logic. Both types repeatedly execute the loop body as long as a specific condition is met. The loop exits when the condition becomes false. The only difference is that the condition is written after the loop body. That means the do…while loop will be always guaranteed to execute at least one iteration.


Pay attention to the semi-colon after the condition.

Let’s rewrite the previous example using the do…while loop.

This modified version should give the same results as the previous one.

Until Loop
Until this condition becomes true, continue to execute the loop body“. This is the logic of the until Loop.


In contrary to the while and do…while loops, the until loop continues to execute the loop body as long as its condition is not met (evaluates to false).

We have written two versions of the script that calculates the total and average of input numbers using the while and do…while loops. Now, let’s see how to do it using the until loop.

As you see, the condition used here is the opposite of the one used in the while and the do…while versions of the script.

Learn the Basics of C Programming Language

Controlling Loops
There will be cases wherein you need to skip executing the remaining lines of the loop body in a loop iteration, or to exit the loop completely. Both cases should be controlled by a condition.

The next Statement
The next statement is used to skip the remaining lines in a loop iteration, and transfer the control back to top of the loop for the next iteration.

Emergency Exit (The last Statement)
The last statement terminates a loop. Once seen, the control is transferred to the next statement after loop’s terminating brace.

We need to write a script that repeatedly prompts the user to enter a number, and tells whether this number is odd or even. If the user enters a zero, the script exits.

Consider the following code:

When executed, it should give an output like the following:

In this article, we have continued with what we started in the article before.

  • The while and do…while loops use similar logic. Both iterate as long as a specific condition is met. When the condition becomes false, the loop exits.
  • The until loop iterates until as specific condition becomes true.
  • Loops can be controlled to either skip executing an iteration, or to exit the loop completely. The decision to skip or exit a loop should be controlled by a condition.

That is it about loops. The next article gets us back to variables, with the second type of variables: Arrays.
See you there.


Please enter your comment!
Please enter your name here