7.5 While and do loops

The while command may be used to continue running a script block until some stopping criterion is met. Two types of while loop are supported:

while <criterion> [ loopname <name> ]
 {
  ....
 }

do [ loopname <name> ]
 {
  ....
 } while <criterion>

In the former case, the enclosed script block is executed repeatedly, and the algebraic expression supplied to the while command is tested immediately before each repetition. If it tests false, then the loop finishes. The latter case is very similar, except that the supplied algebraic expression is tested immediately after each repetition. Thus, the former example may never actually execute the supplied script block if the looping criterion tests false on the first iteration, but the latter example is always guaranteed to run its script block at least once.

The following example would continue looping indefinitely until stopped by the user, since the value 1 is considered to be true:

while (1)
 {
  print "Hello, world!"
 }