until loop - Beginning Perl Tutorial, Control Structures

How to use an until loop in Perl

Woman looking at wall with code
Stanislaw Pytel / Getty Images

Perl's until loop is used to loop through a designated block of code until a specific condition is evaluated as true. It is the logical opposite of the while loop.

 until (expression) {
 	...
 } 

Perl starts the block by evaluating the expression inside the parenthesis. If the expression evaluates as false the code is executed, and will continue to execute in a loop until the expression evaluates as true.

If the expression initially evaluates to true, the code is never executed and the code block will be skipped entirely.

The until loop process looks something like this when you break down each of the steps:

  1. Evaluate the initial expression.
  2. Does the test evaluate to false? If so, continue, otherwise exit the until loop.
  3. Execute the code block inside the until loop.
  4. Return to step 2.

Unlike the for loop, the until loop does not have a self-contained way to alter the initial expression. Be careful that your Perl script does not wind up in a continuous until loop and lock up or crash.

Examples of Perl's Until Loop

As we've discussed, Perl's until loop is used to loop through a designated block of code until a specific condition is evaluated as true. Let's look at an example of Perl's until loop in action and break down exactly how it works, step by step.

 $count = 10;
 until ($count == 0) {
 print "$count ";
 $count--;
 }
 print "Blastoff.\n"; 

Running this simple Perl script produces the following output:

 10 9 8 7 6 5 4 3 2 1 Blastoff. 

First we set the string $count to a value of 10.

 $count = 10; 

Next comes the start of the until loop, and the expression in the parenthesis is evaluated:

 until ($count == 0) 

If the until expression is evaluated as false, the code inside the block is executed and the expression is re-evaluated.

When it finally evaluates as true, the block is skipped and the rest of the Perl script is executed.

  1. $count is set to a value of 10.
  2. Is $count equal to 0? If not, continue, otherwise exit the until loop.
  3. Execute the code block inside the until loop.
  4. Return to step 2.

The end result is that $count starts at 10 and comes down by 1 every time the loop is executed. When we print the value of $count, we can see that the loop is executed until $count has a value equal to 0, at which point the loop stops and the word 'Blastoff' is printed.

  1. An until loop is a Perl control structure.
  2. It is used to step through a block of code until a specific condition is true.
Format
mla apa chicago
Your Citation
Brown, Kirk. "until loop - Beginning Perl Tutorial, Control Structures." ThoughtCo, Mar. 27, 2017, thoughtco.com/until-loop-beginning-control-structures-2641156. Brown, Kirk. (2017, March 27). until loop - Beginning Perl Tutorial, Control Structures. Retrieved from https://www.thoughtco.com/until-loop-beginning-control-structures-2641156 Brown, Kirk. "until loop - Beginning Perl Tutorial, Control Structures." ThoughtCo. https://www.thoughtco.com/until-loop-beginning-control-structures-2641156 (accessed November 20, 2017).