How to stop processing futher code when a condition is met

Hi! How can I stop this script from processing code when F(k-1) is less than or equal to zero, and instead get a message ('the rabbits are instinct')
n = 100;
terskelverdi = 0.1;
F = zeros(n,1);
F(1) = 1;
F(2) = 1;
for k = 3:n;
r = rand(1);
if r > terskelverdi
F(k) = F(k-1) + F(k-2);
else
F(k) = F(k-1) - F(k-2);
end
end

回答 (1 件)

Adam
Adam 2015 年 10 月 14 日

0 投票

Just put the condition at an appropriate place in your loop where you want to do the test:
if F(k-1) <= 0
disp( 'the rabbits are instinct' );
break
end

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

質問済み:

2015 年 10 月 14 日

回答済み:

2015 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by