loop stop executing after executing nested if statement

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 日

1 投票

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 月 21 日
Doesn't work.
madhan ravi
madhan ravi 2018 年 10 月 21 日
what are your operations?
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2018 年 10 月 21 日

コメント済み:

2018 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by