Do while loop in Matlab
Mostra commenti meno recenti
Could you please let me know the Matlab code that is similar to C++ code as shown below:
do {
<your calculations>
} while (abs(A - B) <= 50)
Thanks
2 Commenti
Jan
il 9 Feb 2014
This is no valid C++syntax. Do you mean:
do {
<your calculations>
} while (abs(A - B) <= 50)
MathWorks Support Team
il 27 Nov 2018
We updated the question to reflect correct syntax
Risposta accettata
Più risposte (3)
Jos (10584)
il 9 Feb 2014
A do-while loop in disguise:
while true
% statements here
% if ~WhileCondition, break ; end
end
or
3 Commenti
Timothy Mathias
il 29 Nov 2018
Nice idea Jos. I modified it a bit for my particular needs.
do = true;
while do
% statements here
% do = result_of_conditions_test;
end
Seyedeh Razieh Hosseini
il 5 Gen 2019
The problem here is that you have to calculate twice. our calculation have to be done once before the loop and again inside the loop. that leads to repetition of a part of a code. what should we do in such a case?
David Michelman
il 1 Mag 2020
How so? Since do always starts out as true, you only have to write out the calculation once?
Marco Ottina
il 15 Dic 2022
My suggestion is using the following pattern:
canContinue = true;
while canContinue
% do your code here
canContinue = condition_of_the_do_while ; % insert here your condition
end
Vigneshwar Pesaru
il 17 Set 2017
0 voti
Hi!!!
There is no 'do while' loop in MATLAB in fact you can perform the similar action using 'while' which is powerful in MATLAB
1 Commento
P Richards
il 23 Lug 2019
IHMO The absence of do while makes some coding more difficult than it needs to be:
do
theConditionStillExists=attemptToFixIt();
while theConditionStillExists
Categorie
Scopri di più su Performance and Memory in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!