参考:
01 | 循环语句。 | ||
02 | 循环语句。 | ||
03 | 循环语句。 | ||
04 | 选择语句。 | ||
05 | 只执行符合条件的一个语句块。 | ||
06 | 执行符合条件后面的所有语句块。 | ||
07 | 循环语句。 | ||
08 | 用于显示多条语句。 | ||
09 | 执行循环语句中的下一条循环。 | ||
10 | 从循环、CASE 或 SWITCH 语句中跳出。 |
序号 | 名称 | 功能说明 & 语法 & 举例 | |||
01 | 循环语句。----------------------------------------------------------------------------------------------- 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 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 expression DO statement ----------------------------------------------------------------------------------------------- WHILE expression DO BEGIN statements ENDWHILE -----------------------------------------------------------------------------------------------◈ expression:判断表达式◈ statement(s):循环语句 | ||||
IDL> i = 3IDL> WHILE(i gt 0) DO PRINT, i-- 3 2 1 i = 3 WHILE(i gt 0) DO BEGIN PRINT, i-- ENDWHILE | |||||
04 | 选择语句----------------------------------------------------------------------------------------------- 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 = 2IDL> B = 3IDL> IF(A EQ 2) AND (B EQ 3) THEN PRINT, 'A = ', A, STRING(13B), 'B = ', BA = 2B = 3IDL> A = 2IDL> IF(A EQ 2) THEN PRINT, 'A = ', A ELSE PRINT, 'A ≠ ', AA = 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 | 只执行符合条件的一个语句块。(多条语句需要使用 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' ENDCASEIDL Prints:one x=2 CASE x OF 1: PRINT, 'one' 2: PRINT, 'two' ELSE: PRINT, 'You entered: ', x ENDCASEIDL 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 ENDCASEIDL Prints:You entered: 3Please enter a value between 1 and 2 | |||||
06 | 执行符合条件后面的所有语句块。 ----------------------------------------------------------------------------------------------- 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' ENDSWITCHIDL Prints:onetwo x=2 SWITCH x OF 1: PRINT, 'one' 2: PRINT, 'two' ELSE: PRINT, 'You entered: ', x ENDSWITCHIDL Prints:twoYou 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 ENDSWITCHIDL Prints:twoYou entered: 2Please enter a value between 1 and 2 | |||||
07 | 循环语句(与 WHILE...DO 语句类似)-----------------------------------------------------------------------------------------------REPEAT statement UNTIL expression-----------------------------------------------------------------------------------------------REPEAT BEGIN statementsENDREP UNTIL expression-----------------------------------------------------------------------------------------------◈ statement(s):执行语句◈ expression:判断语句 | ||||
IDL> i = 3IDL> REPEAT PRINT, i-- UNTIL(i lt 0) 3 2 1 0 i = 3 REPEAT BEGIN PRINT, i-- ENDREP UNTIL(i lt 0) | |||||
08 | 用于显示多条语句。-----------------------------------------------------------------------------------------------BEGIN statementsEND | 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 | 执行FOR、WHILE 或 REPEAT 循环语句中下一条循环。 | ||||
FOR I = 1,10 DO BEGIN ; If odd, start next iteration: IF (I AND 1) THEN CONTINUE PRINT, I ENDFORIDL Prints: 2 4 6 8 10 | |||||
10 | 从 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: ', iIDL Prints:Initial value: 0Loop value: 1Loop value: 2Loop value: 3Loop value: 4END VALUE: 5 | |||||