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

2 次查看(过去 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