Flag = 0;
while Flag == 0
for x = 1:10
if x == 3
Flag = 1;
end % end: if
end % end; for
end % end: while
Can anyone tell me why the while loop condition is not triggering when x is three? It runs the entire for loop and I get a value of 10. I am trying to have a flag trigger when a specific condition is met in the for loop.
Thank you!
Flag = 0;
while Flag == 0
for x = 1:10
if x == 3
Flag = 1;
end % end: if
end % end; for
end % end: while
Can anyone tell me why the while loop condition is not triggering when x is three? It runs the entire for loop and I get a value of 10. I am trying to have a flag trigger when a specific condition is met in the for loop.
Thank you!
Share Improve this question asked Mar 27 at 18:30 Alexander SavadelisAlexander Savadelis 214 bronze badges 3 |1 Answer
Reset to default 4If you want to break out of the for
loop early then you should use break
Flag = 0;
while Flag == 0
for x = 1:10
if x == 3
Flag = 1;
break % Exit the 'for' loop
end
end
end
while
loop only checks its guard at the start of each iteration. – Scott Hunter Commented Mar 27 at 18:33break
orreturn
to exit thefor
loop early. – horchler Commented Mar 27 at 20:02