Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
November 03, 2018 Why use while if only iterating once ? | ||||
---|---|---|---|---|
| ||||
while (1) { FLAGS f; switch (*p) { case 'U': case 'u': f = FLAGS.unsigned; goto L1; case 'l': f = FLAGS.long_; error("lower case integer suffix 'l' is not allowed. Please use 'L' instead"); goto L1; case 'L': f = FLAGS.long_; L1: p++; if ((flags & f) && !err) { error("unrecognized token"); err = true; } flags = cast(FLAGS)(flags | f); continue; default: break; } break; } The last break statement prevents the loop from returned for a second iteration. Then why use a while ? |
November 03, 2018 Re: Why use while if only iterating once ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Venkat | On Saturday, November 3, 2018 3:03:16 PM MDT Venkat via Digitalmars-d-learn wrote:
> while (1)
> {
> FLAGS f;
> switch (*p)
> {
> case 'U':
> case 'u':
> f = FLAGS.unsigned;
> goto L1;
> case 'l':
> f = FLAGS.long_;
> error("lower case integer suffix 'l' is not
> allowed. Please use 'L' instead");
> goto L1;
> case 'L':
> f = FLAGS.long_;
> L1:
> p++;
> if ((flags & f) && !err)
> {
> error("unrecognized token");
> err = true;
> }
> flags = cast(FLAGS)(flags | f);
> continue;
> default:
> break;
> }
> break;
> }
>
>
> The last break statement prevents the loop from returned for a second iteration. Then why use a while ?
There's a continue right above the default case. So, if the code hits that point, it will loop back to the top.
- Jonathan M Davis
|
November 03, 2018 Re: Why use while if only iterating once ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Venkat | On Saturday, 3 November 2018 at 21:03:16 UTC, Venkat wrote:
> The last break statement prevents the loop from returned for a second iteration. Then why use a while ?
The continue statement may abort the current iteration and start the next, causing the final break to not necessarily be executed every iteration.
|
November 04, 2018 Re: Why use while if only iterating once ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Thankyou. As the great Gump's mother said, stupid is as stupid does. |
November 06, 2018 Re: Why use while if only iterating once ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Saturday, 3 November 2018 at 21:13:49 UTC, Jonathan M Davis wrote:
> There's a continue right above the default case. So, if the code hits that point, it will loop back to the top.
>
> - Jonathan M Davis
There's also the benefit that FLAGS f exists only until the end of the loop, and thus won't be polluting the namespace later in the function.
Personally, when I want to make a block just for grouping code, I use if (true){} or static if (true){} depending on whether I want to it's variables go away afterwards.
IIRC, I could just use braces without any header intead of if (true){}, but the code block just isn't as easily distinquishable without anything at the top IMO.
|
Copyright © 1999-2021 by the D Language Foundation