loop stop executing after executing nested if statement

1 Ansicht (letzte 30 Tage)
Liu Jian
Liu Jian am 21 Okt. 2018
Kommentiert: Liu Jian am 24 Okt. 2018

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 Kommentar
Liu Jian
Liu Jian am 24 Okt. 2018
turns out I tried to iterate through a column vector, hence the loop stops after the first element.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

madhan ravi
madhan ravi am 21 Okt. 2018
for i = 1:m
do something;
for j = 1:n
do something;
if conditionIsTrue
do something;
else
continue
end
end
end
  4 Kommentare
Liu Jian
Liu Jian am 24 Okt. 2018
thx problem sovled.
madhan ravi
madhan ravi am 24 Okt. 2018
Accept the answer so that other people know the question is solved

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by