Break statement inside an if statement

74 views (last 30 days)
Darragh Tobin
Darragh Tobin on 3 Apr 2020
Commented: Geoff Hayes on 8 Apr 2020
for count = 1:Iteration_lim + 1
%Error check for function not converging
if count == Iteration_lim + 1
error('Iteration limit reached, function failed to converge.')
break
end
This is a section of my code, when I put in the break statement I get an error saying "This statement (and possibly following ones) can not be reached". The code runs and returns a final answer but the break statement does not seem to be doing anything. This is problematic as I want to return the count and the equivalent answer at that iteration, not just the final answer, at the end of the for loop. My professor has uploaded identical code which performs this task without problem. Any help is appreciated.

Answers (2)

Geoff Hayes
Geoff Hayes on 3 Apr 2020
Darragh - the error throws an error and displays a message, so the break will never be called...and so the error message makes sense (I get the same error when using it in my code). To get around this, change the error to an fprintf like
fprintf('Iteration limit reached, function failed to converge.\n');
  11 Comments
Darragh Tobin
Darragh Tobin on 7 Apr 2020
Ok I corrected that mistake but now my function fails to converge for all input values.
Geoff Hayes
Geoff Hayes on 7 Apr 2020
Have you tried stepping through the code to check if the results at each line make sense? Are the angles in angleset reasonable?

Sign in to comment.


Darragh Tobin
Darragh Tobin on 8 Apr 2020
I stepped through all of my code and could still not get the function to converge. My professor supplied this code and said this is how he returned a vlue for count. However, I dont see how it would work as the count on the second line does not appear to do anything and he does not have count as an output. Also, he includes a break statement after an error message which also confuses me.
for count = 1:Iteration_limit + 1
count;
if count == Iteration_limit + 1 % Function returns error if function doesn't convergeerror('Iteration limit reached. Iteration did not converge')
break
end
He claims that the calling the variable count on the second line is sufficient however, when I try to do this I get the error " 'count' produces a value that might be unused.
  1 Comment
Geoff Hayes
Geoff Hayes on 8 Apr 2020
I suspect that "count' produces a value that might be unused." is a warning and not an error so it can be ignored. You are right that having "count;" on the second line does nothing at all. The problem with the convergence probably has nothing to do with this but may instead have something to do with your implementation of the code. Presumably when you say that fails to converge for all input values you mean that you don't get the correct answer and that your code always reaches the maximum number of iterations. Is this correct?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!