What is the syntax for do while loop in matlab?

248 views (last 30 days)
What is the syntax of do while loop in matlab? Can anyone give an example too.
  4 Comments
Guillaume
Guillaume on 13 Jul 2015
It is not a good practice to use while loop
Eh? It is a perfectly good practice to use a while loop when you don't know when the end condition might occur. I'd go so far as to say that using a for loop as you have shown is the bad practice.
Also bad practice is magic arbitrary constant. Particularly in this case, a constant that may or may not be big enough.
Guillaume
Guillaume on 13 Jul 2015
@arun, No, no, no and no! Using for with an arbitrary magic constant for code that may terminate after an indeterminate number of loops is completely wrong. A while loop is the correct way to go.
The usage of arbitrary constants that turn out to be wrong for some input has been the cause of countless number of bugs and security vulnerabilities that continues to plague us to this day (the dreaded buffer overflow).
I've never come across any language that doesn't have a while loop or equivalent. It's there for a purpose, corrupting a for loop for this is wrong.
In your example, you've used an arbitrary maximum number of loops of 2147483647, and possibly the code will work fine now, never hitting that arbitrary value. In 5 years time, the plane that it is controlling will hit 214783648 miles and shut down all engines because the loop ends prematurely, killing all on board.
*If* you want an indeterminate loop to stop after a given number of iterations, then the usual practice is to put a loop counter inside the while loop.

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 13 Jul 2015
A do ... while is simply a while loop which is always evaluated at least once. The simplest way to emulate it is to start the while loop with a true condition and reevaluate the condition at the end of the loop:
condition = true;
while condition
%do something
condition = ... %your while test here.
end
  3 Comments
Guillaume
Guillaume on 14 Jul 2015
What the heck is this obsession with having a while loop ending after a given number of iterations, and how is this failsafe in any way to end on an arbitrary counter value? Possibly, the loop would have ended on its own on the iteration just after your arbitrary limit, but as a result of the premature end of your loop, you've turned off your thrusters too early and crashed your space probe into mars.
I've written control systems for industrial hardware that is never expected to stop except on an error condition. I certainly don't want the machinery to stop working after a given numbers of days no matter how high that is.
There are cases where you may want to end a loop after a given number of iterations (think convergence) but there are plenty more where you want it to end only when whatever end condition has been hit.
As a generic failsafe, a dialog with a stop loop button would be much better than an arbitrary magic number of iterations. Magic numbers have a habit of coming back to bite you years later (think about the fact that we're still limited to 256 characters for path on windows, depending on which API you use).
Jan
Jan on 1 Feb 2017
Edited: Jan on 1 Feb 2017
@Guillaume: You are right, that magic numbers are evail and while loops should not be limited by the number of iterations in general. But this is useful and a good programming practice, when the loop is expected to be finished after a certain number of iterations, but this number cannot be determined before, e.g. when searching for convergence (as you have mentioned already), e.g. Matlab's fminsearch, fzero, lsqr, qmr, bicg and so on.
Another field is the usage of resources, which can be restricted by reasons, which cannot be controlled, e.g. accessing network drives or internet services. Here a timeout and a limited number of re-trying is useful and standard. Although the limits might be chosen arbitrarily, and the process could have been successful just some iterations later, it is better to stop the program with a clear error message, then to be in danger of an unintented infinite loop.
I've seen more programs, which suffered from silently running infinite loops, e.g. caused by dead locks, than software, which failed due to bad limits. While I stop programs using Ctrl-C or the task manager frequently, my work is rarely stopped by a premature loop break.
When I studied, my professor told us:
A program is an algorithm which stops.
While an error message about hitting the iteration limit without getting convergence was accepted as correct result, a WHILE without iteration count was rejected as "algorithm, but not a program" or "magic". But this concerned numerics and not "control systems for industrial hardware".

Sign in to comment.


Maurício Girardi-Schappo
Maurício Girardi-Schappo on 11 Oct 2017
The main difference between a standard while (condition) loop and a do ... while (condition) loop is that the do...while(condition) loop iterates at least once, always.
Thus, it is more straightforward to use a do...while when you don't know the initial state of the variables for the while loop, or if the stop condition or initial state depend on calculations you have to do inside the loop itself.
The closer you can get to a do ... while (condition) loop in MATLAB is
while true
% your code here
if (condition)
break;
end
end
Notice that here condition the loop stop condition (if it is true, the loop stops).
  1 Comment
Walter Roberson
Walter Roberson on 11 Oct 2017
Some people would phrase the above as "do until". "do while(condition)" would be
while true
%your code here
if ~condition
break;
end
end

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!