MATLAB Answers

What is the syntax for do while loop in matlab?

276 ビュー (過去 30 日間)
Anushka
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 件のコメント
Guillaume
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.

サインインしてコメントする。

回答 (2 件)

Guillaume
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.
end
  3 件のコメント
Jan
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".

サインインしてコメントする。


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)
break;
end
end
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
break;
end
end

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by