博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【217】◀▶ IDL 控制语句说明
阅读量:6580 次
发布时间:2019-06-24

本文共 6905 字,大约阅读时间需要 23 分钟。

参考:


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
   statements
ENDREP 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
   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   

执行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
       

转载地址:http://ayino.baihongyu.com/

你可能感兴趣的文章
决心书
查看>>
如何把图片上的文字转换成word?
查看>>
7z命令行
查看>>
C语言编程实现 输入一个非负整数,返回组成它的数字之和(递归方法)
查看>>
c3p0
查看>>
redis cluster 集群搭建(增、删、改、查) :5.0.2
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
引号-下划线,连接多个变量
查看>>
游戏LOGO它应该长什么样?
查看>>
我的友情链接
查看>>
Office365 之分配、回收License工具
查看>>
38线程1-Thread-local-Timer
查看>>
为Exchange server 2013 申请多域名证书
查看>>
处理svn的 File '/aa' is out of date
查看>>
解决 Ubuntu 16.04 LTSSublime text3中文问题
查看>>
mysql主从复制实现数据库同步
查看>>
日期类
查看>>
面试-1
查看>>
CentOS自动登录Gnome
查看>>