I really need to stop the execuation of an If statement during its run.
1 view (last 30 days)
Show older comments
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 Comments
Arpan Bhowmik
on 26 Nov 2021
Edited: Arpan Bhowmik
on 26 Nov 2021
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
Mohamed Abd El Raheem
on 26 Nov 2021
Edited: Mohamed Abd El Raheem
on 26 Nov 2021
Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!