16

I'm planning to use a do-while loop in MATLAB.
Is there a way to do that?

EBH
  • 10,350
  • 3
  • 34
  • 59
Simplicity
  • 47,404
  • 98
  • 256
  • 385

4 Answers4

28
while(true)

%code

    if condition==false
        break; 
    end 
end
CodyF
  • 4,977
  • 3
  • 26
  • 38
Abhishek Thakur
  • 16,337
  • 15
  • 66
  • 97
  • I assume that this gives a similar functionality. However it is not exectly a do while loop. Still, it is no big deal I guess. – patrik Apr 02 '15 at 08:18
  • 3
    So is it the case that Matlab has no formal do while construct? – jxramos Jun 17 '15 at 23:00
  • `condition==false` is more clearly and succinctly written as `~condition`. This notation makes it clear that we're negating the condition. – Cris Luengo Jun 07 '23 at 14:39
3

Here's another option in MATLAB (closer to a do-while syntax):

do = true;
while(do || condition)
    do = false;
    % things to do...
end
EBH
  • 10,350
  • 3
  • 34
  • 59
  • 1
    Be careful with this. If you use `continue` before setting `do = false`, then `condition` will be ignored. I would make `do = false` the first line of the while loop. – Paul Wintz Jun 06 '23 at 23:06
  • @PaulWintz you are right. I have edited it accordingly. – EBH Jun 07 '23 at 05:46
1

At least, Octave has do-until. This example creates a variable fib that contains the first ten elements of the Fibonacci sequence.

fib = ones (1, 10);
i = 2;
do
  i++;
  fib (i) = fib (i-1) + fib (i-2);
until (i == 10)

Of course, you must invert your abortion condition compared to do-while.

winkmal
  • 622
  • 1
  • 6
  • 16
-1

MATLAB does not have a do-while construct (nor a do-until construct, in MATLAB the condition is always at the top of the loop). But you can always easily rewrite a do-while construct to a while-do construct.

For example, say you have a loop that iterates a computation until convergence. It is written as a do-while construct as follows:

result = 0
do
   new_result = ...
   difference = abs(new_result - result)
   result = new_result
while difference > tol

You can write exactly the same thing with a while-do loop as follows:

result = 0
difference = Inf
while difference > tol
   new_result = ...
   difference = abs(new_result - result)
   result = new_result
end

The only extra thing we needed to do was initialize difference to a value that guarantees that the loop runs at least once.

This is similar to EHB's answer in that they also converted the do-while construct into a while-do construct. But in general it is not necessary to create a separate sentinel variable to do so.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Of course, a do-while loop is not necessary, but in many cases it is convenient, hence the OP's question (and the presence of do-while loops in most languages). In some cases, using a while instead of a do-while requires code duplication. Telling people to simply not try to use do-while is not helpful. – Paul Wintz Jun 07 '23 at 21:19
  • @PaulWintz do-while doesn't exist in MATLAB, I'm not telling people to not try to use it, I'm showing people how to encode the same logic with the available tools in the cleanest possible way. – Cris Luengo Jun 07 '23 at 21:22
  • Sure, and a while loop works fine for the example you gave, but in more complicated examples, a do-while loop allows us to avoid duplication, in which case the other answers are preferable. – Paul Wintz Jun 08 '23 at 08:38
  • @PaulWintz No, it doesn’t. Give me your more complex example and I’ll show you. – Cris Luengo Jun 08 '23 at 12:37