Section 10.2. The until Control Structure


10.2. The until Control Structure

Sometimes, you'll want to reverse the condition of a while loop. To do that, use until:

     until ($j > $i) {       $j *= 2;     } 

This loop runs until the conditional expression returns true. It's really just a while loop in disguise, except it repeats as long as the conditional is false, rather than true. The conditional expression is evaluated before the first iteration, so this is a zero-or-more-times loop, just like the while loop.[*] As with if and unless, you could rewrite any until loop to become a while loop by negating the condition. But generally, you'll find it simple and natural to use until from time to time.

[*] Pascal programmers, take note: in Pascal, the repeat-until always runs at least one iteration, but an until loop in Perl may not run at all if the conditional expression is true before the loop starts.



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net