digitalmars.D.learn - Why use while if only iterating once ?
- Venkat (32/32) Nov 03 2018 while (1)
- Jonathan M Davis (5/37) Nov 03 2018 There's a continue right above the default case. So, if the code hits th...
- Venkat (2/2) Nov 03 2018 Thankyou.
- Dukc (11/14) Nov 06 2018 There's also the benefit that FLAGS f exists only until the end
- lithium iodate (4/6) Nov 03 2018 The continue statement may abort the current iteration and start
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 ?
Nov 03 2018
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
Nov 03 2018
Thankyou. As the great Gump's mother said, stupid is as stupid does.
Nov 03 2018
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 DavisThere'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.
Nov 06 2018
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.
Nov 03 2018