Skip to Content
Learning Perl on Win32 Systems
book

Learning Perl on Win32 Systems

by Randal L. Schwartz, Erik Olson, Tom Christiansen
August 1997
Beginner
312 pages
8h 35m
English
O'Reilly Media, Inc.
Content preview from Learning Perl on Win32 Systems

The do {} while/until Statement

The while/until statement you saw in the previous section tests its condition at the top of every loop, before the loop is entered. If the condition was already false to begin with, the loop won’t be executed at all.

But sometimes you don’t want to test the condition at the top of the loop. Instead, you want to test it at the bottom. To fill this need, Perl provides the do {} while statement, which is just like the regular while [41] statement except that it doesn’t test the expression until after executing the loop once. For example:

do {
    statement_1;
    statement_2;
    statement_3;
} while (some_expression);

Perl executes the statements in the do block. When it reaches the end, it evaluates the expression for truth. If the expression is false, the loop is done. If it’s true, then the whole block is executed one more time before the expression is once again checked.

As with a normal while loop, you can invert the sense of the test by changing do {} while to do {} until. The expression is still tested at the bottom, but its sense is reversed. For some cases, especially compound ones, this is the more natural way to write the test:

$stops = 0;
do {
    $stops++;
    print "Next stop? ";
    chomp($location = <STDIN>);
} until $stops > 5 || $location eq 'home';

[41] Well, this statement is not quite true; the loop control directives explained in Chapter 9, Miscellaneous Control Structures, don’t work for the bottom-testing form.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Beginning Perl for Bioinformatics

Beginning Perl for Bioinformatics

James Tisdall
Perl for System Administration

Perl for System Administration

David N. Blank-Edelman

Publisher Resources

ISBN: 1565923243Catalog PageErrata