Problem with break statement

I have a code:
for i=1:n
for j=1:n
if 'some condition'
'some logic'
if 'some condition'
break
end
end
end
end
now my question is that if the 2nd if statement is true and it breaks shouldnt it end the 2nd for loop since if statement is under 2nd for loop but whenever I run this if the 2nd if statement is true and it breaks it ends the 1st for loop.
Any guidance is highly appreciated.

 Accepted Answer

Adam Danz
Adam Danz on 17 Nov 2020
Edited: Adam Danz on 17 Nov 2020
> ...shouldn't it end the 2nd for loop since if statement is under 2nd for loop... ?
Yes, it should end the 2nd loop and that's what it's doing. To better understand what's going on, I inserted fprintf commands in each loop so you can see the iteration numbers. The first condition is true when j==2 and the second nested condition is true when i==3 (while j is equal to 2).
clc
n = 4;
for i=1:n
j = NaN;
fprintf('\n i=%d, j=%d',i,j)
for j=1:n
fprintf('\n i=%d, j=%d',i,j)
if j==2
fprintf(' CONDITION 1')
if i==3
fprintf(' & CONDITION 2: breaking')
break
end % end i==2 test
end % end j==2 test
end
end
fprintf('\n')
You can see (below) that the break condition occurs when i==3 and j==2 and on the next iteration, the i-loop interates to i==4 and the j-loop resets.

6 Comments

Hey Adam,
Thanks for answering my question and clearing my doubt.
As can be seen in the command window that when the break occurs j goes to NaN again and our code starts to run from i=whatever_is_next. what I want to do is whenever this break occurs I want to continue with j=whatever_is_next. can you give a solution for this. if there is a way please consider answering.
Adam Danz
Adam Danz on 17 Nov 2020
Edited: Adam Danz on 17 Nov 2020
If I understand you correctly, you just need to remove the break.
By the way, j doesn't really turn to NaN, I just depicted it that way to show that the i-loop has reset and therefore the j-loop will too.
Or do you want to iterate to the next i but then start on what would have been the next j in the previous i-iteration?
If so, try this,
jHolder = -inf;
for i=1:n
j = NaN;
for j=1:n
if condition1
% Do something
if condition2 && j>jHolder
% Do something
jHolder = j;
break
end
end
end
end
fprintf('\n')
Hey Adam,
after that break statement I have a huge line of codes so what I want to do is skip those line of codes and continue with the same i and next possible j. like for example if i=1 and j=2 and when it enters the if loop and the consition is true it should break and not execute all the lines below break and continue again with i=1 and j=3. is there a solution for this.
I am a beginner in MATLAB now so this help will be highly appreciated.
Thanks in advance.
Ahhhhhh...... what you're looking for in the continue function.
If that was described better from the start, this answer would have been just a few words! 😀
So, replace break with continue.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 17 Nov 2020

Commented:

on 18 Nov 2020

Community Treasure Hunt

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

Start Hunting!