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

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

ELSE BEGIN

ENDELSE

IF (0) THEN A=1 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

IF expression THEN BEGIN

ENDIF

IF (0) THEN BEGIN

   A=1

ENDIF

REPEAT BEGIN

ENDREP

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

LABEL: BEGIN

END

LABEL1: BEGIN

   PRINT, A

END

case_expression: BEGIN

END

CASE name OF

'Moe': BEGIN

         PRINT, 'Stooge'

       END

ENDCASE

switch_expression: BEGIN

END

SWITCH name OF

'Moe': BEGIN

         PRINT, 'Stooge'

       END

ENDSWITCH

CASE and SWITCH also have their own END identifiers. CASE should always be ended with ENDCASE, and SWITCH should always be ended with ENDSWITCH.

Version History

Original

Introduced

See Also

BREAK, CASE, CONTINUE, FOR, FOREACH, GOTO, IF...THEN...ELSE, REPEAT...UNTIL, SWITCH, WHILE...DO,IDL Programming