What is the difference between " while 1" and "while true", Should I use one over the other?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
NeGM
am 26 Jul. 2022
Bearbeitet: James Tursa
am 26 Jul. 2022
I'd like to know the difference between while 1 and while true and in which cases I should use one over the other. Thank you
0 Kommentare
Akzeptierte Antwort
James Tursa
am 26 Jul. 2022
Bearbeitet: James Tursa
am 26 Jul. 2022
1 is a double class scalar and true is a logical class scalar, so the check for "non-zero" is slightly different for each even though they both accomplish the same thing. For all we know the parser might even optimize this away into a simple GOTO at the end of the loop. But since you are using the value in a logical context, using true probably makes more sense. I suppose checking a 1-byte integer value in the background (logical) might be ever so slightly faster than checking an 8-byte floating point value in the background (double) also, although for an interpreted language like MATLAB I doubt you could tell the difference in timing.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!