What is the syntax for do while loop in matlab?
293 views (last 30 days)
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;
condition = ... %your while test here.
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
% your code here
Notice that here condition the loop stop condition (if it is true, the loop stops).