Does && has higher precedence than || in if?

조회 수: 3 (최근 30일)
Iila
Iila 2016년 1월 15일
댓글: the cyclist 2016년 1월 15일
Recently I used:
if p==0&&q==0&&r==0||p==0&&q==0||q==0&&r==0||p==0&&r==0
if p>=0&&q>=0&&r>=0||p<=0&&q<=0&&r<=0
if a>0&&b>0&&c>0||d>0&&e>0&&f>0
if g>0&&h>0
whereas I actually mean:
if (p==0&&q==0&&r==0)||(p==0&&q==0)||(q==0&&r==0)||(p==0&&r==0)
if (p>=0&&q>=0&&r>=0)||(p<=0&&q<=0&&r<=0)
if (a>0&&b>0&&c>0)||(d>0&&e>0&&f>0)
if (g>0&&h>0)
The problem is I have done months of calculations without brackets, thingking && has higher precedence than . Now I just reflected: is it true? Was my code right?
Please tell me if my code was correct without brackets or not before I publish my calculation results. This is a serious matter to me if not; I'm really worried.
P.S. I'm aware of short circuits.
  댓글 수: 3
Guillaume
Guillaume 2016년 1월 15일
It is also trivial to check for yourself:
0 && 0 || 1
If it returns 1, it means it's been evaluated as
(0 && 0) || 1
If it returns 0, it's been evaluated as
0 && (0 || 1)
the cyclist
the cyclist 2016년 1월 15일
This test does not distinguish between the following two rules:
  • && has precedence over ||
  • && and || have equal precedence, with left-to-right evaluation

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2016년 1월 15일
According to this page about order precedence, && does indeed have precedence over ||.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by