OR gives invalid results

조회 수: 1 (최근 30일)
HARI
HARI 2022년 3월 21일
편집: Stephen23 2022년 3월 21일
Hello,
Please see the attached picture.
Its very simple but i dont get the correct results.Can anyone clarify why it outputs 12 instead of 22?
  댓글 수: 1
Stephen23
Stephen23 2022년 3월 21일
편집: Stephen23 2022년 3월 21일
"OR gives invalid results"
Actually OR gives exactly the expected result.
"Can anyone clarify why it outputs 12 instead of 22?"
Following the rules of precedence given here:
we can see that EQ has a higher precedence than short-circuiting ||, so your code
left == 1 || 3
is equivalent to:
(left == 1) || 3
which will always be TRUE because 3 is non-zero and so is considered TRUE.
"Its very simple but i dont get the correct results"
Yet MATLAB is just doing exactly what you told it to do. You need to pay attention to the operator precedence!

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

채택된 답변

Torsten
Torsten 2022년 3월 21일
if left == 1 || left == 3
...
The condition "3" is empty and thus true. MATLAB does not relate the "3" to "left".
  댓글 수: 1
HARI
HARI 2022년 3월 21일
oh My bad!Thank you so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by