I really need to stop the execuation of an If statement during its run.
조회 수: 1 (최근 30일)
이전 댓글 표시
Greetings...
Here is the situation.
1- You have a "master" condition that gets you inside an if statement which does a bunch of calculations.
2- Sometimes, the calculations done inside that if generates a secondary conditions which tells you to stop these calculations immediately go outside the big If and continue the code.
Since the break statement is confined to breaking loops, it cannot be used here.
Any suggestions?
A code example:
auto = input("Do you want to do auto convergence? "); % A logical input from the user to do auto converge calculations
if auto
maxTimes = 10^5;
loopCount = 0;
test = true;
while test
[test, data] = autoConvergeFunction(data); % the function gives false if no more is required
% and also updates the computation data
loopCount = loopCount + 1;
if loopCount == maxTimes % did not reach conevrgence and must stop
break; % so we stop the while loop, but we want to exit the big IF, How can we do it?
end
end
result = moreCalculations(data); % We want to skip the rest of the big IF in case we reach
% maxTimes without converge.
end
댓글 수: 2
Arpan Bhowmik
2021년 11월 26일
편집: Arpan Bhowmik
2021년 11월 26일
One possible solution:
Take the body of the calculations and put it in a separate function. Have your if-condition call that function. In that function, when the condition to terminate calculations is triggered, use return to return control to the calling body, which is the if-condition. Your code will resume in the if-condition (if you only have the call to calculating function, control will continue past the end of the if-condition).
Update
With your code excerpt, you can try the following:
auto = input("Do you want to do auto convergence? "); % A logical input from the user to do auto converge calculations
if auto
data = doAutoConvergenceTillMaxstep();
end
function data = doAutoConvergenceTillMaxstep()
maxTimes = 10^5;
loopCount = 0;
test = true;
while test
[test, data] = autoConvergeFunction(data); % the function gives false if no more is required
% and also updates the computation data
loopCount = loopCount + 1;
if loopCount == maxTimes % did not reach conevrgence and must stop
return; % Return to the main body if-condition
end
end
result = moreCalculations(data); % We want to skip the rest of the big IF in case we reach
% maxTimes without converge.
end
A second, simpler solution, if what you are doing is looping till some max number of loops, replace the while loop with a for-loop. You can then do the more-calculations which you want to skip if convergence was not reached in an if condition on the iteration variable.
auto = input("Do you want to do auto convergence? "); % A logical input from the user to do auto converge calculations
if auto
maxTimes = 10^5;
loopCount = 0;
test = true;
for iter=1:maxTimes
[test, data] = autoConvergeFunction(data); % the function gives false if no more is required
% and also updates the computation data
end
if iter < maxTimes
result = moreCalculations(data); % We want to skip the rest of the big IF in case we reach
% maxTimes without converge.
else
disp('Convergence not reached');
end
end
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!