Do while loop in Matlab
이전 댓글 표시
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
Jan
2014년 2월 9일
This is no valid C++syntax. Do you mean:
do {
<your calculations>
} while (abs(A - B) <= 50)
MathWorks Support Team
2018년 11월 27일
We updated the question to reflect correct syntax
채택된 답변
추가 답변 (3개)
Jos (10584)
2014년 2월 9일
A do-while loop in disguise:
while true
% statements here
% if ~WhileCondition, break ; end
end
or
댓글 수: 3
Timothy Mathias
2018년 11월 29일
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
2019년 1월 5일
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
2020년 5월 1일
How so? Since do always starts out as true, you only have to write out the calculation once?
Marco Ottina
2022년 12월 15일
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
2017년 9월 17일
0 개 추천
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
P Richards
2019년 7월 23일
IHMO The absence of do while makes some coding more difficult than it needs to be:
do
theConditionStillExists=attemptToFixIt();
while theConditionStillExists
카테고리
도움말 센터 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!