2014-09-01 11 views
2

我使用朱莉婭0.3.0在Windows 8.1朱莉婭語言按位或if語句

我輸入下面的:

julia> Y, M = 2000, 2 
(2000,2) 

julia> if M == 1 | M == 2 
      Y -= 1 
      M += 12 
     end 

julia> Y, M 
(2000,2) 

我預計Y = 1999年,M = 14

顯然這不是使用按位或(|)的方式 如何實現所需的結果?

回答

3

使用||http://docs.julialang.org/en/latest/manual/control-flow/#man-short-circuit-evaluation)用於控制流程or

這種情況下的問題是運算符優先級:|是按位或高於等於的,請參閱http://docs.julialang.org/en/latest/manual/mathematical-operations/。例如,它適用於(M==1)|(M==2)。不清楚優先級是否應該改變,至少people are talking about the issue

+0

很好很多謝謝 – 2014-09-01 17:48:03

+0

你能接受答案嗎? – IainDunning 2014-09-01 18:04:32