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 CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by