I really need to stop the execuation of an If statement during its run.

4 views (last 30 days)
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
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
Mohamed Abd El Raheem on 26 Nov 2021
Edited: Mohamed Abd El Raheem on 26 Nov 2021
I want to exit the calling IF condition !
As I clarified in the question, the rest of the master if condition must be skipped.
Update:
Since I am not always running the calculations up to maxTimes, I will not use the second solution.
The first solution is fine, with one slight modification. If the auto convergence is stopped at maxTimes without converge, I shall include a logical output to indicate that, since the rest of the calling code would execute in an alternative scheme.
Any coments about the following?
auto = input("Do you want to do auto convergence? "); % A logical input from the user to do auto converge calculations
NoConv = false; % Assuming no need to go to the other scheme by default
if auto
[data, NoConv] = doAutoConvergenceTillMaxstep();
end
if ~auto || NoConv % If it is not required to be done in auto OR
% the auto is not converged
% do it another way
end
function [data, NoConv] = doAutoConvergenceTillMaxstep()
NoConv = false;
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
NoConv = true;
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
And thanks for responding!

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!