Hi I have been told you can convert any for loop to a while loop. If so I don't see how you could convert the following to a while loop?
for i = 1:5
disp(10 * i);
end
Thanks.

답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 6월 19일

3 개 추천

I'm not sure why a while loop would be better in this case (because it adds a couple of more lines of code) but here is the while equivalent to the above for loop
k = 1;
while k<=5
disp(10*k);
k=k+1;
end
Note that with this code, we have to manage the initialization of k, the incrementing of k and ensure that the condition has been set correctly.

댓글 수: 1

Giuseppe
Giuseppe 2014년 6월 19일
Thanks I know it would not be better although I just wanted so see how the code could be written another way.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2014년 6월 19일

댓글:

2014년 6월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by