how to give end condition to for loop

6 ビュー (過去 30 日間)
Rakesh R
Rakesh R 2019 年 3 月 3 日
回答済み: Sheng Chen 2019 年 3 月 3 日
for e = (-246200:-246100)
for f = 224169
e
f
h = ((e*def)+f)
if h==138010
return;
end
end
end
  1 件のコメント
Rik
Rik 2019 年 3 月 3 日
And your question is?
There are multiple things that I would have written differently, but depending on the context this code should work.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2019 年 3 月 3 日
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');

その他の回答 (1 件)

Sheng Chen
Sheng Chen 2019 年 3 月 3 日
You already terminate your code by "return" if h equals 138010.
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by