Previous Application Programming: Program Control Next

Compound Statements

Many of the language constructs that we will discuss in this chapter evaluate an expression, then perform an action based on whether the expression is true or false, such as with the IF statement:

IF expression THEN statement  

For example, we would say "If X equals 1, then set Y equal to 2" as follows:

IF (X EQ 1) THEN Y = 2  

But what if we want to do more than one thing if X equals 1? For example, "If X equals 1, set Y equal to 2 and print the value of Y." If we wrote it as follows, then the PRINT statement would always be executed, not just when X equals 1:

IF (X EQ 1) THEN Y = 2   
PRINT, Y  

IDL provides a container into which you can put multiple statements that are the subject of a conditional or repetitive statement. This container is called a BEGIN...END block, or compound statement. A compound statement is treated as a single statement and can be used anywhere a single statement can appear.

BEGIN...END

The BEGIN...END statement is used to create a block of statements, which is simply a group of statements that are treated as a single statement. Blocks are necessary when more than one statement is the subject of a conditional or repetitive statement.

For example, the above code could be written as follows:

IF (X EQ 1) THEN BEGIN  
   Y = 2  
   PRINT, Y  
END  

All the statements between the BEGIN and the END are the subject of the IF statement. The group of statements is executed as a single statement. Syntactically, a block of statements is composed of one or more statements of any type, started by BEGIN and ended by an END identifier. To be syntactically correct, we should have ended our block with ENDIF rather than just END:

IF (X EQ 1) THEN BEGIN  
   Y = 2  
   PRINT, Y  
ENDIF  

This is to ensure proper nesting of blocks. 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.

 

Table 7-1: Types of END Identifiers

Table 7-1: Types of END Identifiers
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  
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  


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

The IDL compiler checks the end of each block, comparing it with the type of the enclosing statement. Any block can be terminated by the generic END, but no type checking is performed. Using the correct type of END identifier for each block makes it easier to find blocks that you have not properly terminated.

Listings produced by the IDL compiler indent each block four spaces to the right of the previous level to make the program structure easier to read. (See .RUN for details on producing program listings with the IDL compiler.)

  IDL Online Help (March 06, 2007)