while loops test the condition before every execution of the loop. Sometimes you want it to be tested after the loop. This guarantees that the loop is run once before the condition is tested.

  print "$value\n";
} while($value>10);

The above code prints out:
Then it tests $value which is now equal to 4 to see if it greater than 10. Since it is not the code within the loop is not run again

Next find out about do until loops

Replies are listed 'Best First'.
RE: do while loops
by Anonymous Monk on May 24, 2000 at 05:04 UTC
    need a semicolon at end of first line "$value=5"
      Couldn't $value = $value - 1; be written like this?:

      $value -= 1; # or $value-- ; # since its just -1

      - Jim

        Yeah, they could.
        I think this example is trying to limit itself to the syntax of the do/while loop, though. So it can be fully understood on its own. Using the 'minimalist' decrement operators might distract someone new to the language.
Re: do while loops
by Anonymous Monk on Oct 24, 2004 at 03:41 UTC

    ok.. the code is bad. That situations cause problems to novices

    $value=5 <------ ok that is the problem it does not have ; do{ print "$value\n"; $value=$value-1; } while($value>10);

    ok the correct code is

    $value=5; # now have ; do{ print "$value\n"; $value=$value-1; } while($value>10);

    have a fun

    20041024 Edit by ysth: change p tags to code

      Actually, I think the problem is larger than that.
      shouldn't it be:
      $value = 15; # 15, not 5. 5 doesn't make sense if we're not increment +ing. do { print "$value\n"; $value = $value - 1; } while($value > 10);

      jdporter fixed formatting

        The point was that the test happens at the end, so the loop executes once even if the value starts out < 10.

        Caution: Contents may have been coded under pressure.
Re: do while loops
by sapnac (Beadle) on Dec 01, 2006 at 15:18 UTC
    What do you do if you want to exit out of the loop earlier? This should be covered under this topic as it differs from one to another. Last; -> to break out next; -> to skip to next

      do{ ... } while $foo; isn't a loop, it's a block with a postfixed while, so you need to label it to re-do it.

      @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;
        sum = 0; for (x = 1; x <= 5; x++) { printf("%d", x); sum = sum + x; printf("The sum is %d", sum); }