IDL程序控制(四)


注:程序控制主要是学习 格式哦~

循环语句

1.for

pro test_li
for i=0,3 do begin
  print,i
endfor
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
       0
       1
       2
       3
IDL> 

2.while

pro test_li
i=0
while (i lt 3) do begin
  print,i
  i++
endwhile
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
       0
       1
       2
IDL> 

3.repeat

pro test_li
i=0
repeat begin
  print,i
  i++
endrep until (i gt 3)
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
       0
       1
       2
       3
IDL> 

4.foreach

pro test_li
arr1=indgen(3)*2
print,arr1
foreach element,arr1 do begin
  print,element
endforeach
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
       0       2       4
       0
       2
       4
IDL> 

条件语句

if

if 判断的表达形式多样,但是有其书写规律,请君自行思考
形式1:

pro test_li
a=5
if a eq 5 then print,'a 等于5'
end

形式2:

pro test_li
a=5
if a eq 5 then begin
  print,'a 等于5'
  print,'可以执行多行'
endif
end

形式3:

pro test_li
a=5
if a lt 3 then begin
  print,'a 小于3'
  print,'可以执行多行'
endif else begin
  print,'a 不小于 3 '
  print,'可执行多行'
endelse
end

形式4:

pro test_li
a=4
if a lt 3 then begin
  print,'a 小于3'
  print,'可以执行多行'
endif else if a gt 5 then begin
print,'a 大于 5'
endif else begin
  print,'a 既不小于 3,也不大于5 '
endelse
end

case

注:注意区分case和switch的区别

pro test_li
index=2
case index of 
  0:print,'indet=0'
  1:print,'index=1'
  2:print,'index=2'
  3:print,'index=3'
endcase
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
index=2
IDL> 

switch

case只执行对应index的操作
switch执行对应index及之后的操作

pro test_li
index=2
switch index of 
  0:print,'indet=0'
  1:print,'index=1'
  2:print,'index=2'
  3:print,'index=3'
endswitch
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
index=2
index=3
IDL> 

跳转语句

break

跳出整个循环

pro test_li
for i= 1,5 do begin
  print,i
  if i eq 3 then break
endfor
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
       1
       2
       3
IDL> 

continue

跳出本次循环,继续执行下一次循环

pro test_li
for i= 1,5 do begin
  if i eq 3 then continue
  print,i
  
endfor
end

结果:

IDL> test_li
% Compiled module: TEST_LI.
       1
       2
       4
       5
IDL> 

goto

常用在选择文件过程中
若直接输入文件名,则在改文件中操作
若在文件夹中选择特定文件,弹出问题:是否替换该文件,若是,则删掉原文件,若否,则重新选择文件

pro test_li
jump2:
file=dialog_pickfile()
if file_test(file) then begin
  result=dialog_message('是否替换现有文件',/question)
  if result eq 'Yes' then begin
    file_delete,file
  endif else begin
    goto,jump2
  endelse 
  
endif
  

print,file
end
发布了39 篇原创文章 · 获赞 5 · 访问量 3057

猜你喜欢

转载自blog.csdn.net/weixin_43955546/article/details/105661962