0

I was trying to make a simple statement with Matlab as follows:

if TF==1
  disp('One'), break
else continue
end
... ... ...
... ... ...

But even if TF is not 1, when I run the command, it doesn't CONTINUE to the rest of the script!! Any help would be appreciated-- Thanks

2
  • No need for that else continue, just remove that line. Commented Jan 12, 2013 at 4:45
  • Is this 'if' inside a loop? The continue statement would actually discard the present iteration and start a fresh one. It will not proceed after the line of code its present in. Break would exit the loop. Commented Jan 12, 2013 at 4:47

1 Answer 1

3

The continue statement has a very different meaning. Within a loop, like a for or while loop, continue instructs to skip the current round and continue with the next iteration in the loop. So if you remove continue, you will see the behavior that you are expecting. Here is an example:

for k = 1 : 10
  if k == 4
    % skip the calculation in the case where k is 4
    continue
  end
  area = k * k;
  disp(area);
end

When the loop iterates at k == 4, the block calculating the area of the corresponding square is skipped. This particular example is not very practical.

However, imagine you have a list of ten file names, and you want to process each file in this loop "for k = 1 : 10". You will have to try and open each file, but then if you find out the file does not exist, an appropriate way to handle it would be to print a little warning and then continue to the next file.

Sign up to request clarification or add additional context in comments.

2 Comments

and break is also intended to be used in a loop: it quits the loop altogether.
Thank you so much, I miscomprehended 'continue'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.