continue, how to skip part of a cycle

조회 수: 6 (최근 30일)
OldCar
OldCar 2016년 10월 5일
댓글: OldCar 2016년 10월 6일
I have this code, that is supposed to skip to the following k value when count reaches 3
clear
clc
p=0;
for k=1:10
count=0;
while k-p>3
k
p=p+0.25;
count=count+1;
if count>=2
continue
end
end
end
but it doesn't happen.
What haven't I understood?
Many thanks

채택된 답변

James Tursa
James Tursa 2016년 10월 6일
편집: James Tursa 2016년 10월 6일
Your if-test and continue statement are inside the inner while loop, so they apply to that loop, not the for-loop. Maybe use a break to get out of the while loop if that is what you want to do. (The way you have it coded, the continue statement does nothing since it simply goes to the next iteration of the while loop and isn't skipping over any code)
  댓글 수: 1
OldCar
OldCar 2016년 10월 6일
Many thanks, short but clear!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by