a < b < c < d, what does this Boolean return for different values of a, b, c and d?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Manu Krishnan
am 9 Nov. 2017
Bearbeitet: Jan
am 10 Nov. 2017
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 Kommentare
John D'Errico
am 9 Nov. 2017
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
am 10 Nov. 2017
Bearbeitet: Jan
am 10 Nov. 2017
@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.
Akzeptierte Antwort
Steven Lord
am 9 Nov. 2017
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 Kommentare
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!