Terminating a loop or case

break

Terminate a loop or case


SYNTAX

break

The break command may only be used within a for, while or do loop, or in the case section of a switch statement.

In loops, the break command terminates the current iteration of the loop and passes control to the statement that follows the loop’s terminating right brace.

In a switch statement, a break command terminates the current case and passes control to the statement following the switch’s terminating right brace.


NOTES

  • If this command is encountered outside a for, while, do or switch statement, the compiler error “break must be inside a loop statement” is generated.

  • The special label BREAK may be used within the trap clause of a Sculptor command to perform the same function.


EXAMPLE

  1. Using the break command in a switch construct. The break command is necessary at the end of each case to prevent control simply falling through to the next case:

    switch (tmp.Index) {
         case = "R":
              ProcessByRef()
              break
    
         case = "N":
              ProcessByName()
              break
    
         default:
              info "Invalid index"
              break
    }
    
  2. Using the break command in a while construct. Here, the program loops until a zero value is encountered in the subscripted field tmp.Value:

    while(TRUE) {
         setcol +1
         if (tmp.Value = 0) then break
         tmp.Total = tmp.Total + tmp.Value
    }
    
  3. This function returns the first element of an array field matching the argument:

    !function GetFieldNo(FieldName) {
    !temp Loop,,i2
    !temp FieldNo,,i2
    
         FieldNo = 0
         for (Loop = 1; Loop <= NumDDFields; Loop ++ ) {
              if (FieldName = FieldProps.Name[Loop]) {
                   FieldNo = Loop
                   break
              }
         }
         return FieldNo
    }
    

RELATED TOPICS

breakif

BREAK