Moving to the next iteration of external loop from inside the nested loop

40 views (last 30 days)
I have a nested loop with 4 for loop and in the internal loop I am doing a condition check and if that condition check is satisfied I want to jump to the next iteration of the external loop.
Something like this;
for yy=1:10
for month=1:12
for day=1:31
for UTM= 0:30:1410
if( condition )
% Move to next iteration of first or external loop( here, go for the next day)
%I want to skip doing the work1 in the continue
end
% some work being done
work1;
end
end
end
end
I used "break" in the if condition but it doesn't work and it has been continued with doing work1

Accepted Answer

Image Analyst
Image Analyst on 25 Oct 2021
I believe this should do it:
for yy = 1 : 10
skipIt = false;
for month=1:12
for day=1:31
for UTM= 0:30:1410
if( condition )
% Move to next iteration of first or external loop( here, go for the next day)
%I want to skip doing the work1 in the continue
skipIt = true;
break; % break out of 4th loop.
end
% some work being done
work1;
end
if skipIt
break; % break out of 3rd loop.
end
end
if skipIt
break; % break out of 2nd loop.
end
end
end

More Answers (0)

Categories

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

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!