Repeating statements while a condition is true (while)

while

Repeat statements while a condition is true


SYNTAX

while (condition) {
    statements
}

The while command repeats the statements while the condition evaluates as true. The statements to be executed are enclosed within left and right braces.

condition

The condition to be tested. It may include all supported arithmetical, relational and logical operators. These are listed in Table of operators together with their order of precedence. Parentheses may be used to force a different order of evaluation.

If condition is an expression without a conditional test (e.g. 100; 34 + 6; “ABC”; tmp.FlagField), it is evaluated in the following way:

Numeric

An expression that evaluates to zero is false; one that evaluates to anything else is true.

Alphanumeric

An expression that produces a null string, or a string containing only spaces, is false; all other alphanumeric expressions are true.

statements

A block of Sculptor code, enclosed within left and right braces.

The condition is evaluated at the start of each iteration of the loop. If it is false, the loop terminates immediately and control is passed to the statement following the closing brace.

If the condition evaluates as false at the start of the first iteration, the loop is not entered. Note the difference between this and the do… while command, where the statements are always executed at least once.

The special commands break and continue may only be used inside a loop.

The break command causes the loop to terminate and passes control to the statement following the right brace.

The continue command causes the current iteration to end. Statements between the continue command and the right brace are skipped. Control passes to the test of condition to determine whether further loops are executed.

An expression that evaluates to zero is false, while one that evaluates to anything else is true.


NOTES

  • The simple expression 1 may be used as a condition and will cause a loop to repeat indefinitely. The only way to terminate such a loop is the break command. The pre-defined manifest constant TRUE could also be used.

  • The statements between the braces may contain other loop or switch constructs (for, switch, while or do).

  • Within a loop, a Sculptor command that uses trap clauses can use the special labels BREAK and CONTINUE. These have exactly the same effect as the break and continue commands.


EXAMPLE

  1. Count the selected lines in a table:

    !function CountSelLines(Table) {
    !temp Row,,i2
    !temp NumSelLines,,i4
    
         NumSelLines = 0
    
         Row = 1
         while (Row <= Table->max_line) {
              if (is_selected(Table, Row)) then NumSelLines ++
              Row ++
         }
    
         return NumSelLines
    }
    
  2. Get data from a sequential file. The loop can only be ended when the err trap is generated:

    while (TRUE) {
         get #1, tmp.OutputLine[tmp.NumOutputLines + 1] err = BREAK
         tmp.NumOutputLines++
    }
    

RELATED TOPICS

do

break

continue