loop stop executing after executing nested if statement

조회 수: 1 (최근 30일)
Liu Jian
Liu Jian 2018년 10월 21일
댓글: Liu Jian 2018년 10월 24일

I wrote a nested loop like below:

   for i = 1:m
      do something;
      for j = 1:n 
        do something;
        if conditionIsTrue
          do something;  %without continue or break
        end
      end
    end

But the loop ends after the first time the condition in if statement is executed and return incorrect result, I tried to modify the code as below:

    for i = 1:m
      do something;
      for j = 1:n 
        do something;
        if conditionIsTrue
          do something;  
          continue
        else
          continue
        end
      end
    end
The problem's still there. How can I deal with this problem?
  댓글 수: 1
Liu Jian
Liu Jian 2018년 10월 24일
turns out I tried to iterate through a column vector, hence the loop stops after the first element.

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

답변 (1개)

madhan ravi
madhan ravi 2018년 10월 21일
for i = 1:m
do something;
for j = 1:n
do something;
if conditionIsTrue
do something;
else
continue
end
end
end
  댓글 수: 4
Liu Jian
Liu Jian 2018년 10월 24일
thx problem sovled.
madhan ravi
madhan ravi 2018년 10월 24일
Accept the answer so that other people know the question is solved

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

카테고리

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