www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why use while if only iterating once ?

reply Venkat <venkatram.akkineni gmail.com> writes:
         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
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
next sibling parent Venkat <venkatram.akkineni gmail.com> writes:
Thankyou.

As the great Gump's mother said, stupid is as stupid does.
Nov 03 2018
prev sibling parent Dukc <ajieskola gmail.com> writes:
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.
Nov 06 2018
prev sibling parent lithium iodate <whatdoiknow doesntexist.net> writes:
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