필터 지우기
필터 지우기

How many times this for loop will execute

조회 수: 1 (최근 30일)
Pramit Biswas
Pramit Biswas 2016년 4월 30일
답변: Roger Stafford 2016년 4월 30일
count =0;
for i=1:5
count = count +1;
if true
i=i+1;
end
end
I want to skip some loop value depending upon conditions, for demo, I used every next time it should skip. But this is not happening. This loop executes all the 5 times.
1. Why?
2. What to do to achieve this?
for the time being for skipping 1 step used this code:
count =0; f= false;
for i=1:5
if f
f = false;
continue;
end
count = count +1;
if true
f=true;
end
end
but this is really awkward and somehow -ve for MATLAB, didn't expect. %HeartBroken%

답변 (1개)

Roger Stafford
Roger Stafford 2016년 4월 30일
As to the “why” the for-loop does not accept any changes you might make to its variable, in this case ‘i’, as it repeats. So even though you changed 'i' internally it will keep on steadily using its own count for 'i'. It you want to skip out of the loop ahead of its prescribed sequence, use the ‘break’ command.

카테고리

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