Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>They'll only be equivalent if the first thing after breaking out of the loop is a <code>return</code> statement.</p> <p>In any case, if your lecturer doesn't allow <code>break</code>, they probably won't allow <code>return</code> either.</p> <p>You should be aware that these rules have a good basis behind them (to prevent spaghetti code and make it easier to write maintainable code) but sometimes they're enforced over-zealously.</p> <p>For example, there's <em>nothing</em> unreadable about the segment:</p> <pre><code>if (x == 0) return 0; return x - 1; </code></pre> <p>even though it has multiple <code>return</code> statements.</p> <p>The supposedly preferred solution to the no-multiple-return crowd is something like:</p> <pre><code>int y = x - 1; if (x == 0) y = 0; return y; </code></pre> <p>which is, in my opinion, both less readable and a waste of space.</p> <p>The real problems occur when you have very complex logic and the hoops that people jump through to avoid <code>break</code> and/or <code>return</code> leads to conditionals that are darn-near unreadable.</p> <p>Listen to your lecturer - they are, after all, the ones in control of what grade you get. Then, once you get into the real world, you can switch from dogmatism to pragmatism and make up your own mind.</p> <p>See <a href="https://stackoverflow.com/questions/216359/break-statements-in-the-real-world/216375#216375">here</a> for some good advice regarding these issues outside of educational institutions.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload