BEGIN...END
The BEGIN...END statement defines a block of statements. A block of statements is a group of statements that is treated as a single statement. Blocks are necessary when more than one statement is the subject of a conditional or repetitive statement.
Syntax
BEGIN
statements
END | ENDIF | ENDELSE | ENDFOR | ENDFOREACH | ENDREP | ENDWHILE | ENDCASE | ENDSWITCH
The END identifier used to terminate the block should correspond to the type of statement in which BEGIN is used. The following table lists the correct END identifiers to use with each type of statement.
Statement |
END Identifier
|
Example |
IF expression THEN BEGIN
|
ENDIF |
IF (1) THEN BEGIN
A=1
ENDIF
|
ELSE BEGIN |
ENDELSE |
IF (0) THEN BEGIN
A=1
ENDIF ELSE BEGIN
A=2
ENDELSE
|
FOR variable=init, limit DO BEGIN
|
ENDFOR |
FOR i=1,5 DO BEGIN
PRINT, array[i]
ENDFOR
|
FOREACH element, variable [, key] DO BEGIN
|
ENDFOREACH |
arr = [1, 3, 5, 7, 9]
FOREACH element, arr DO BEGIN
PRINT, element
ENDFOREACH
|
REPEAT BEGIN |
ENDREP UNTIL |
REPEAT BEGIN
A = A * 2
ENDREP UNTIL A GT B
|
WHILE expression DO BEGIN
|
ENDWHILE |
WHILE ~ EOF(1) DO BEGIN
READF, 1, A, B, C
ENDWHILE
|
CASE var OF |
ENDCASE |
CASE name OF
...
ENDCASE
|
case_expression: BEGIN
|
END |
CASE name OF
'Moe': BEGIN
PRINT, 'Stooge'
END
ENDCASE
|
SWITCH var OF |
ENDSWITCH |
SWITCH name OF
...
ENDSWITCH
|
switch_expression: BEGIN
|
END |
SWITCH name OF
'Moe': BEGIN
PRINT, 'Stooge'
END
ENDSWITCH
|
Version History
See Also
BREAK, CASE,
CONTINUE, FOR,
FOREACH, GOTO,
IF...THEN...ELSE,
REPEAT...UNTIL,
SWITCH,
WHILE...DO, IDL Programming