if-elseif-else statement take turns/used each of them in while loop, how to write properly?

1 回表示 (過去 30 日間)
Hello, im new in MATLAB, and here's my problem: the while loop should be run at sometime, while each loop should take the if statement sequentially (while loop do work> take if statement>second loop>take elseif > 3rd loop> take else> 4th loop> if statement> and so on) i kinda new to use some of the feature like rem feature. Could you give me some guidance? thanks :)
i=1
a(i)=0
while true
i = i+1
a(i) = a(i-1) +1
if a==1:3:15
disp('its first num')
elseif a== 2:3:15
disp('its sec num')
else a==3:3:15
disp('its 3rd num')s
end
if a > 20
break
end
end

採用された回答

Voss
Voss 2022 年 6 月 30 日
Like this?
i=0;
while i <= 20
i = i+1;
r = rem(i,3);
if r == 1
disp('its first num')
elseif r == 2
disp('its sec num')
else
disp('its 3rd num')
end
end
its first num
its sec num
its 3rd num
its first num
its sec num
its 3rd num
its first num
its sec num
its 3rd num
its first num
its sec num
its 3rd num
its first num
its sec num
its 3rd num
its first num
its sec num
its 3rd num
its first num
its sec num
its 3rd num

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by