alex_bn_lee

导航

【217】◀▶ IDL 控制语句说明

参考:Statements Routines —— 控制语句关键字


01   FOR 循环语句
02   FOREACH 循环语句
03   WHILE...DO 循环语句
04   IF...THEN...ELSE 选择语句
05   CASE 只执行符合条件的一个语句块。 
06   SWITCH
执行符合条件后面的所有语句块
07   REPEAT...UNTIL 循环语句。 
08   BEGIN...END 用于显示多条语句
09   CONTINUE  执行循环语句中的下一条循环。
10   BREAK 从循环、CASE 或 SWITCH 语句中跳出。

序号 名称  

功能说明 & 语法 & 举例

01 FOR  

循环语句。
-----------------------------------------------------------------------------------------------

 FOR variable = init, limit [, Increment] DO statement 
-----------------------------------------------------------------------------------------------
 FOR variable = init, limit [, Increment] DO BEGIN 
    statements                                     
 ENDFOR                                           
-----------------------------------------------------------------------------------------------
◈ init:开始的数
◈ limit:结束的数
◈ Increment:增量值
◈ statement(s):循环语句内容

 
IDL> FOR i = 1, 10, 4 DO PRINT, i
       1
       5
       9

  FOR i = 1, 10, 4 DO BEGIN
    PRINT, i
  ENDFOR
 02 FOREACH  

循环语句。
-----------------------------------------------------------------------------------------------
 
FOREACH Element, Variable [, Index] DO Statement 
-----------------------------------------------------------------------------------------------
 FOREACH Element, Variable [, Index] DO BEGIN 
    statements                                
 ENDFOREACH                  
-----------------------------------------------------------------------------------------------
? Element:每一个元素
◈ Variable:数组变量
◈ Index:元素的索引值
◈ Statement(s):循环语句内容

 
IDL> strs = ["alex", "bn", "lee"]
IDL> FOREACH e, strs, i DO PRINT, i, "--", e
                     0--alex
                     1--bn
                     2--lee

  strs = ["alex", "bn", "lee"]
  FOREACH e, strs, i DO BEGIN
    PRINT, i, "--", e
  ENDFOREACH
03 WHILE...DO  

  循环语句。
-----------------------------------------------------------------------------------------------
 WHILE
expression DO statement 
-----------------------------------------------------------------------------------------------
 WHILE
expression DO BEGIN 
    statements             
 ENDWHILE                  
-----------------------------------------------------------------------------------------------
◈ expression:判断表达式
◈ statement(s):循环语句

 
IDL> i = 3
IDL> WHILE(i gt 0) DO PRINT, i--
       3
       2
       1

  i = 3
  WHILE(i gt 0) DO BEGIN
    PRINT, i--
  ENDWHILE
04 IF...THEN...ELSE  

选择语句
-----------------------------------------------------------------------------------------------
 IF
expression THEN statement 
-----------------------------------------------------------------------------------------------
 IF
expression THEN statement ELSE statement 

-----------------------------------------------------------------------------------------------
 IF
expression THEN BEGIN 
   statements             
 ENDIF                    
-----------------------------------------------------------------------------------------------
 IF
expression THEN BEGIN 
   statements             
 ENDIF ELSE BEGIN         
   statements             
 ENDELSE                  

-----------------------------------------------------------------------------------------------
◈ expression:判断表达式

◈ statement(s):语句内容

 
IDL> A = 2
IDL> B = 3
IDL> IF(A EQ 2) AND (B EQ 3) THEN PRINT, 'A = ', A, STRING(13B), 'B = ', B
A =        2
B =        3

IDL> A = 2
IDL> IF(A EQ 2) THEN PRINT, 'A = ', A ELSE PRINT, 'A ≠ ', A
A =        2

  A = 2
  B = 3
  IF(A EQ 2) AND (B EQ 3) THEN BEGIN
    PRINT, 'A = ', A
    PRINT, 'B = ', B
  ENDIF

  A = 2
  IF(A EQ 2) THEN BEGIN
    PRINT, 'A = ', A
  ENDIF ELSE BEGIN
    PRINT, 'A ≠ ', A
  ENDELSE
05 CASE  

只执行符合条件的一个语句块。(多条语句需要使用 BEGIN...END)
------------------------------------------------------------
 CASE expression OF           
     expression: statement(s) 
     ...                      
     expression: statement(s) 
 ENDCASE                      
------------------------------------------------------------
 CASE
expression OF           
     expression: statement(s) 
     ...                      
     expression: statement(s) 
   ELSE: satement(s)          
 ENDCASE                      
------------------------------------------------------------
 CASE
expression OF           
     expression: statement(s) 
     ...                      
     expression: statement(s) 
   ELSE: BEGIN                
     satement(s)              
   END                        
 ENDCASE                      
------------------------------------------------------------
◈ expression:判断语句
◈ statement(s):执行语句

 
  x=1
  CASE x OF
     1: PRINT, 'one'
     2: PRINT, 'two'
  ENDCASE

IDL Prints:
one

  x=2
  CASE x OF
     1: PRINT, 'one'
     2: PRINT, 'two'
     ELSE: PRINT, 'You entered: ', x
  ENDCASE

IDL Prints:
two

  x=3
  CASE x OF
     1: PRINT, 'one'
     2: PRINT, 'two'
     ELSE: BEGIN
        PRINT, 'You entered: ', x
        PRINT, 'Please enter a value between 1 and 2'
     END
  ENDCASE

IDL Prints:
You entered: 3
Please enter a value between 1 and 2
06
SWITCH
 

执行符合条件后面的所有语句块。       
-----------------------------------------------------------------------------------------------
 SWITCH
expression OF         
     expression: statement(s) 
     ...                      
     expression: statement(s) 
 ENDSWITCH                    
-----------------------------------------------------------------------------------------------
 
SWITCH expression OF         
     expression: statement(s) 
     ...                      
     expression: statement(s) 
   ELSE: satement(s)          
 ENDSWITCH                    

-----------------------------------------------------------------------------------------------
 SWITCH
expression
OF         
     expression: statement(s) 
     ...                      
     expression: statement(s) 
   ELSE: BEGIN                
     satement(s)              
   END                               
 ENDSWITCH                   

-----------------------------------------------------------------------------------------------
◈ expression:判断语句
◈ statement(s):执行语句

 
  x=1
  SWITCH x OF
     1: PRINT, 'one'
     2: PRINT, 'two'
  ENDSWITCH

IDL Prints:
one
two

  x=2
  SWITCH x OF
     1: PRINT, 'one'
     2: PRINT, 'two'
     ELSE: PRINT, 'You entered: ', x
  ENDSWITCH

IDL Prints:
two
You entered:        2

  x=2
  SWITCH x OF
     1: PRINT, 'one'
     2: PRINT, 'two'
     ELSE: BEGIN
        PRINT, 'You entered: ', x
        PRINT, 'Please enter a value between 1 and 2'
     END
  ENDSWITCH

IDL Prints:
two
You entered:        2
Please enter a value between 1 and 2
07 REPEAT...UNTIL  

循环语句(与 WHILE...DO 语句类似)
-----------------------------------------------------------------------------------------------
REPEAT statement UNTIL expression
-----------------------------------------------------------------------------------------------
REPEAT BEGIN
   statements
ENDREP UNTIL expression
-----------------------------------------------------------------------------------------------
◈ statement(s):执行语句
◈ expression:判断语句

 
IDL> i = 3
IDL> REPEAT PRINT, i-- UNTIL(i lt 0)
       3
       2
       1
       0

  i = 3
  REPEAT BEGIN
    PRINT, i--
  ENDREP UNTIL(i lt 0)
08 BEGIN...END  

用于显示多条语句。
-----------------------------------------------------------------------------------------------
BEGIN
   statements
END | ENDIF | ENDELSE | ENDFOR | ENDFOREACH | ENDREP | ENDWHILE
-----------------------------------------------------------------------------------------------

 
  CASE name OF
    'Moe': BEGIN
      PRINT, 'Stooge'
    END
  ENDCASE

  SWITCH name OF
    'Moe': BEGIN
      PRINT, 'Stooge'
    END
  ENDSWITCH
09  CONTINUE  

执行FOR、WHILE 或 REPEAT 循环语句中下一条循环。

 
  FOR I = 1,10 DO BEGIN
   ; If odd, start next iteration:
   IF (I AND 1) THEN CONTINUE
   PRINT, I
  ENDFOR

IDL Prints:
       2
       4
       6
       8
      10
10  BREAK  

从 FOR、WHILE、REPEAT 循环、CASE 或 SWITCH 语句中跳出。

 
  i = 0
  PRINT, 'Initial value: ', i
  WHILE (1) DO BEGIN
    i = i + 1
    IF (i eq 5) THEN BREAK
    PRINT, 'Loop value: ', i
  ENDWHILE
  PRINT, 'END VALUE: ', i

IDL Prints:
Initial value:        0
Loop value:        1
Loop value:        2
Loop value:        3
Loop value:        4
END VALUE:        5
       

posted on 2016-08-24 09:33  McDelfino  阅读(7731)  评论(0编辑  收藏  举报