Doug's MATLAB Video Tutorials

March 19th, 2010

Break and Continue

This video covers two flow control commands that do not get that much use, but can make your code clearer and easier to write.

9 Responses to “Break and Continue”

  1. Kate replied on :

    Oh no – did you change the way you show videos? Is it all on youtube now? My work blocks youtube, but I’ve been able to watch all the previous videos. I can see this video at all. What changed? Thanks for any help.

  2. dhull replied on :

    @Kate,

    We are in the process of moving to YouTube for distribution. I would like to hear from people if this is a good idea. The polling we did earlier showed that it would give us a wider audience.

    Please, let us know!

    Doug

  3. Kate replied on :

    @Doug,

    Sorry, the YouTube wouldn’t work for me at all. I can’t watch YT at work (have to make nice with the IT folks), and I generally don’t do work at home. Is there any way to have a link to a non-YT option in addition to the normal YT movie?

    Thanks for listening,
    Kate

  4. Ondrej replied on :

    @Doug

    from my point of view moving videos to YT is not a good idea as it is blocked at most companies. I use Matlab only at work so I would prefer the way it was before.

  5. Aurélien replied on :

    Idem. Youtube is blocked in my company.

  6. Scott replied on :

    Why bother with the “continue” command? It can be simplified to:

    if dieRoll < target
        break
    end
    

    Continue is implied by the while loop.

  7. dhull replied on :

    @scott,

    Yes, the continue is gratuitous. However, I like it for clarity.

    I will very often have all the options covered explicitly even if the code would just ‘fall through’ and do the same thing. That way when I am reading the code and I am thinking “What happens if X is less than Y?” I don’t have to work out that it just continues, it is explicitly in there.

    In some way, it is just kind of a comment in the code. But you are right, for the most part, it is a no-op.

    Doug

  8. safa replied on :

    hello
    what should one do if there is two while(or for)loop exist?i want the program to stop counting when a if condition fulfilled in the inner for loop..sth like goto command.could you please guide me?

  9. dhull replied on :

    @Safa,

    If resturcturing does not work, you could set a flag and break. The flag you set would then trigger a break out of the outer loop.

    Doug

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.