Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are times when using <code>goto</code> is actually the RIGHT answer - at least to those who are not brought up in the religious belief that "<code>goto</code> can never be the answer, no matter what the question is" - and this is one of those cases. </p> <p>This code is using the hack of <code>do { ... } while(0);</code> for the sole purpose of dressing up a <code>goto</code> as a <code>break</code>. If you are going to use <code>goto</code>, then be open about it. It's no point in making the code HARDER to read. </p> <p>A particular situation is just when you have a lot of code with quite complex conditions:</p> <pre><code>void func() { setup of lots of stuff ... if (condition) { ... ... if (!other condition) { ... if (another condition) { ... if (yet another condition) { ... if (...) ... } } } .... } finish up. } </code></pre> <p>It can actually make it CLEARER that the code is correct by not having such a complex logic. </p> <pre><code>void func() { setup of lots of stuff ... if (!condition) { goto finish; } ... ... if (other condition) { goto finish; } ... if (!another condition) { goto finish; } ... if (!yet another condition) { goto finish; } ... .... if (...) ... // No need to use goto here. finish: finish up. } </code></pre> <p>Edit: To clarify, I'm by no means proposing the use of <code>goto</code> as a general solution. But there are cases where <code>goto</code> is a better solution than other solutions. </p> <p>Imagine for example that we are collecting some data, and the different conditions being tested for are some sort of "this is the end of the data being collected" - which depends on some sort of "continue/end" markers that vary depending on where you are in the data stream. </p> <p>Now, when we're done, we need to save the data to a file. </p> <p>And yes, there are often other solutions that can provide a reasonable solution, but not always. </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