a < b < c < d, what does this Boolean return for different values of a, b, c and d?

조회 수: 1 (최근 30일)
if I have an if statement: if a < b < c < d
what is the behavior here where a, b, c and d are numeric. Is there an order of operation?
  댓글 수: 2
John D'Errico
John D'Errico 2017년 11월 9일
Well, depending on the values of a, b, c, and d, it returns the result:
a < b < c < d
I'll give you hint as to the result. It returns either 0 or 1.
What order do you think applies? What is the standard in MATLAB? (Hint: left to right?)
Come on. You really should do your own homework.
Jan
Jan 2017년 11월 10일
편집: Jan 2017년 11월 10일
@Manu Krishnan: I understand that "Come on. You really should do your own homework." does not directly help you. But it is not offending. Therefore I have removed the flag.
I understand "come on" as: "It is time to realize, that you can solve this easier by your own, than you think." Matlab is a programming language for solving scientific problems. On one hand reading the manuals is obligatory, on the other hand you can examine what Matlab does simply by experiments. John tried to encourage you to find the solution by your own. Sometimes newcomers hesitate to recognize this option.
Is there an order of operation?
Yes, Manu, of course there is. I cannot even imagine, how a programming language could work without an order of operations. A simple test reveals the details. Define the variables accordingly and type this in the command window:
a < b < c < d
a < b < c
a < b
What do you see? If "a < b" replies 1 or 0, what is the meaning of "a < b < c"? Matlab does show you directly, in which order the expression is evaluated. Therefore it is easier to ask Matlab instead of the forum.

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

채택된 답변

Steven Lord
Steven Lord 2017년 11월 9일
As stated on this documentation page, all the relational operators are at the same level of precedence. Therefore they are evaluated left to right as stated at the beginning of that page.
This does not, as you might think, return true if a, b, c, and d are in strictly ascending order. To test that condition I would use the issorted function on the vector [a b c d] with one of the direction flags.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by