### until loops

by root (Scribe)
 on Nov 05, 1999 at 04:36 UTC Need Help??

Until loops are just the opposite of whiles. If the control statement evaluates to false code within the loop is executed once and repeated until the control statement tests to true.
Heres a quick example:
```\$year=1900;
until(\$year==2000){
print "Mission-critical electronics working in the year \$year\n";
#now it works;
\$year++;
}
print "Not working anymore\n";
```
As you can see the mission-critical electronics work until the year 2000.
I suggest if you've just survived that harrowing ordeal you learn all about do while loops

Replies are listed 'Best First'.
RE: until loops
by turnstep (Parson) on May 24, 2000 at 05:01 UTC
The "\$year++" part was added later, *after* the first couple replies.
Re: until loops
by Frey (Novice) on May 13, 2001 at 20:34 UTC
Actually, this is not an infinite loop. This loop is always false until it evaluates 2000,
in which case it is true, and it stops. I got hung up on this too, I think the backwardness of until can be confusing.
I dont think that is correct, the expression "\$year==2000" will be false if \$year is not 2000 but the actual statement of unless(\$year==2000) will be true unless year equals 2000. Also an infinite loop is one where the condition will never be met and so will never terminate, in this case if year will never become 2000. For example, if year was initialised to 2001 or if year was 1999 and never incremented then it is an infinite loop.
RE: until loops
by Anonymous Monk on May 05, 2000 at 20:27 UTC
If I'm not mistaking that until loop is an infinite loop due to not having an increment.
RE: until loops
by Simplicus (Monk) on May 22, 2000 at 21:57 UTC
Yes, and your loop will still be executing in the year 3000.
Simplicus
The increment takes place with "\$year++"
so does \$year++ mean that year gets incremented by 1 ?? or 2 ?
Re: until loops
by lvanhout (Curate) on Jun 16, 2004 at 05:11 UTC
I usually find it a little cleaner to say
```until(\$year >= 2000)
That is to say continue until year is greater than or equal to 2000. In case you go back and change your code within the loop and do something more complicated than increment.

Unless you really mean that only 2000 is an escape loop case and 2001 and up will be continue loop cases.
On the other hand, with only the == instead of >=, it's a perfectly appropriate demonstration of the concept. I'm not sure, but if compiled, the == might actually execute faster than the >=, though by such a minute margin that it's effectively useless to differentiate.

- apotheon
Unless vs. Until?
by inblosam (Monk) on May 22, 2002 at 06:53 UTC
Can't you just use an "unless" just the same? Is there an advantage/disadvantage to one or the other?

```\$year=1900;
unless(\$year==2000){     #notice unless instead of until
print "blah blah \$year\n";
#now it works;
\$year++;
}
print "Done\n";

Michael Jensen
InShift Technologies
http://www.inshift.com
michael@inshift.com

Unfortunately this won't work. The until loop will continue redoing the block until the condition is met. Unless will only execute the block one time, leaving the counter at 1901 instead of 2000.

Update: crazyinsomniac has pointed out a better way to say this: The unless(condition) statement is saying the same thing as to if( not condition). The until(condition) statement is equivelant to while( not condition).

That is to say, until() and while() are loops, and will repeat until the condition is met, while if() and unless() are not, and will only execute the block 0 or 1 times. Therefore, you can't trade unless() for until().

Create A New User
Node Status?
node history
Node Type: perltutorial [id://949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2019-10-24 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2019 the site I miss most is:

Results (58 votes). Check out past polls.

Notices?