# how to give end condition to for loop

35 views (last 30 days)
Rakesh R on 3 Mar 2019
Answered: Sheng Chen on 3 Mar 2019
for e = (-246200:-246100)
for f = 224169
e
f
h = ((e*def)+f)
if h==138010
return;
end
end
end
Rik on 3 Mar 2019
There are multiple things that I would have written differently, but depending on the context this code should work.

Image Analyst on 3 Mar 2019
Try this:
bailOut = false;
def = -1
for e = -246200:-246100
for f = 138000 : 224169
h = ((e*def)+f);
fprintf('e = %d, f = %d, h = %d\n', e, f, h);
if h >= 138010 % Or h == 138010 if you're really super certain!
bailOut = true;
break; % Break out of the f loop.
end
end
% Bail out of the e loop, if needed
if bailOut
break;
end
end
fprintf('Done!\n');

Sheng Chen on 3 Mar 2019
I assume you are asking how to terminate the inner for loop:
for f = 224169
....
end
You can put a "break" in this for loop: Terminate execution of for or while loop
for e = (-246200:-246100)
for f = 224169
e
f
h = ((e*def)+f)
if h==138010
break;
end
end
end