Continue for nested loops

조회 수: 33 (최근 30일)
Chai-tea
Chai-tea 2019년 7월 13일
댓글: Basil C. 2019년 7월 13일
This is my code snippet below
for jj = 1:5
f(x)=...
for ii = 16:20
f(x)=...
continue
end
end
What I want to do: I want to run ii loop once, then go to the next iteration of jj and continue with the next iteration of ii.
My question: Could I use the 'continue' command to stop the ii loop after first iteration, go back to continue the jj loop, when I am in jj loop, continue with the second iteration of the ii loop and so on. Something like shown below
jj = 1
f(x)=...
ii = 16
f(x)=...
jj = 2
f(x)=...
ii = 17
f(x)=...
jj = 3
f(x)=...
ii = 18
f(x)=...
I hope my question makes sense. Thanks!

답변 (1개)

Basil C.
Basil C. 2019년 7월 13일
편집: Basil C. 2019년 7월 13일
You could try out this
for jj = 1:5
f(x)=...
for ii = 15+j:20
f(x)=...
continue
end
end
but if you want to run ii loop once then why even use FOR loop, you could try something like
for jj = 1:5
f(x)=...
ii = 15+j
f(x)=...
end
  댓글 수: 2
Chai-tea
Chai-tea 2019년 7월 13일
I get it now, not using a second loop makes sense. thanks!
Basil C.
Basil C. 2019년 7월 13일
Your welcome! If the answer was right please accept the answer.

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

카테고리

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