If statement idl elseif问题/混乱

If statement idl elseif问题/混乱,if-statement,idl-programming-language,If Statement,Idl Programming Language,我正试图将一个简单的elseif语句放入IDL中,我对它非常满意。matlab代码看起来像这样 a = 1 b = 0.5 diff = a-b thres1 = 1 thres2 = -1 if diff < thres1 & diff > thres2 'case 1' elseif diff > thres1 'case 2' elseif diff < thres2 'case 3' end 所以我想出来了。对于我们这些刚接触IDL

我正试图将一个简单的elseif语句放入IDL中,我对它非常满意。matlab代码看起来像这样

a = 1
b = 0.5

diff = a-b
thres1 = 1
thres2 = -1

if diff < thres1 & diff > thres2  
  'case 1'
elseif diff > thres1 
  'case 2'
elseif diff < thres2
  'case 3'
end

所以我想出来了。对于我们这些刚接触IDL语言的人来说

在我看来,IDL对于每个if语句只能处理2种情况,所以我不得不在另一个“if”块中编写

希望这能帮助其他人

a = 1;
b = 2.5;

diff = a-b;
thres1 = 1;
thres2 = -1;

if diff gt thres1 then begin
  print,'case 1'
endif

if (diff lt thres2) then begin
  print,'case 2'
  endif else begin
  print,'case 3'
endelse 

IDL中没有
elseif
语句。尝试:

a = 1
b = 0.5

diff = a - b
thres1 = 1
thres2 = -1

if (diff lt thres1 && diff gt thres2) then begin
  print, 'case 1'
endif else if (diff gt thres1) then begin
  print, 'case 2'
endif else if (diff lt thres2) then begin
  print, 'case 3'
endif

mgalloy的答案是正确的,但您可能也会看到一些人(比如我),他们在只有一行时不使用begin/endif。(当然,如果有人返回并试图插入一行,却没有意识到你做了什么,这会导致问题,因此Michael的方法可能更好……这只是为了当你看到这种格式时,你会意识到它在做同样的事情:

if (diff lt thres1 && diff gt thres2) then $
  print, 'case 1' $
else if (diff gt thres1) then $
  print, 'case 2' $
else if (diff lt thres2) then $
  print, 'case 3'
或者是一种可能使某人不太容易插入的格式:

if      (diff lt thres1 && diff gt thres2) then print, 'case 1' $
else if (diff gt thres1)                   then print, 'case 2' $
else if (diff lt thres2)                   then print, 'case 3'

如果任何值等于阈值,则不会执行任何情况。是的,你是对的。我应该说,造成我问题的不是逻辑,而是实际语法。IDL不会编译和运行我正在展示的代码示例。谢谢mgalloy!很抱歉,我的答复太晚了,我正在休假,刚刚回去工作.2013年快乐!
if      (diff lt thres1 && diff gt thres2) then print, 'case 1' $
else if (diff gt thres1)                   then print, 'case 2' $
else if (diff lt thres2)                   then print, 'case 3'