我正试图在IDL中获得一个简单的elseif语句,并且对它有一段时间了。 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代码并不那么简单,我在解决语法问题上遇到了麻烦。帮助说明: 句法 IF表达式THEN语句[ELSE语句] 要么 如果表达那么开始 声明 ENDIF [ELSE BEGIN 声明 ENDELSE]
但是没有举例说明如何使用多个表达式和elseif。我尝试了许多变化,似乎无法做到正确。
有人有建议吗?以下是我尝试过的一些事情:
if (diff lt thres1) and (diff gt thres2) then begin
print, 'case 1'
endif else begin
if (diff gt thres1) then
print, 'case 2'
endif else begin
if (diff lt thres2) then
print, 'case 3'
endif
if (diff lt thres1) and (diff gt thres2) then begin
print, 'case 1'
else (diff gt thres1) then
print, 'case 2'
else (diff lt thres2) then
print, 'case 3'
endif
答案 0 :(得分:3)
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
答案 1 :(得分:0)
所以我明白了。对于我们这些刚接触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
答案 2 :(得分:0)
mgalloy的回答是正确的,但是你也可能会看到那些只有一行时不使用begin / endif的人(比如我)。 (当然,这会导致问题,如果有人回去试图插入一条线,没有意识到你做了什么,所以迈克尔的方法可能更好......这只是为了当你看到这种格式时,你意识到它正在做同样的事情:
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'