MATLAB Answers

What is the syntax for do while loop in matlab?

561 ビュー (過去 30 日間)
Anushka 2015 年 7 月 13 日
コメント済み: Walter Roberson 2017 年 10 月 11 日
What is the syntax of do while loop in matlab? Can anyone give an example too.

  4 件のコメント

表示 1 件の古いコメント
arun 2015 年 7 月 13 日
for i1=1:1:2147483647 %size of for loop is based on application, mainly to get rid of inf loop
if i1>b %condition at end; This is a junk program
U can use, for i1=1:1:inf but u will get warning message. Please note that "break" in "if" statement is mandatory else this will become a bad program.
Guillaume 2015 年 7 月 13 日
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 2015 年 7 月 13 日
@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.

回答 (2 件)

Guillaume 2015 年 7 月 13 日
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.

  3 件のコメント

Image Analyst
Image Analyst 2015 年 7 月 14 日
A robust while loop should always have a failsafe. This would be some number that is way more iterations than you ever expect to have, so that you don't get infinite loops. If you ever reach that number then you know for a fact that something went wrong. It can be built into the condition
condition = true;
numIterations = 0;
maxIterations = 1000000; % More than you ever expect to have.
while condition
%do something
% Increment loop counter.
numIterations = numIterations + 1;
% Recompute condition for continuing.
condition = (numIterations < maxIterations) & ... %your while test here.
Guillaume 2015 年 7 月 14 日
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 2017 年 2 月 1 日
@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 2017 年 10 月 11 日
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)
Notice that here condition the loop stop condition (if it is true, the loop stops).

  1 件のコメント

Walter Roberson
Walter Roberson 2017 年 10 月 11 日
Some people would phrase the above as "do until". "do while(condition)" would be
while true
%your code here
if ~condition

Sign in to comment.


Translated by