Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Do you think do-while statements are ugly?

Do you think do-while statements are ugly?

Scheduled Pinned Locked Moved The Lounge
comtutorialquestionannouncement
20 Posts 11 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Vitaly Tomilov

    If you are looking for a more readable form, then why not use a for-loop? They are not more elegant, but easier to read, especially to an inexperienced developer. Other than that, your code snippets 1 and 3 are fine, while 2 is ugly (bad code styling). Another variation, that you didn't consider, would be:

    int num = 0;
    do
    {
    Console.WriteLine("Number: " + num.ToString());
    }
    while(num++ < 5);

    This also got me thinking, C++ and C# are missing a simpler form of a loop that doesn't need a variable. However, it is too easy to implement yourself. Example:

    // repeat n times, or until break is encountered:
    repeat(n)
    {
    }

    Would such operator make life easier? ;)

    Let's agree to disagree! Boris the animal Just Boris.

    M Offline
    M Offline
    Mike Marynowski
    wrote on last edited by
    #7

    Also, how would you propose implementing it yourself? I don't see a way to add it with C# in a way that is seamless and doesn't look incredibly hacky with lambdas and such.

    1 Reply Last reply
    0
    • M Mike Marynowski

      This isn't for cases that can be covered with the for-loop, I use that (and foreach) whenever possible - my example is contrived and simplified. I didn't intend on an example that is better suited for a for loop. Usually my condition is something like stream.HasMoreData() or something of the sort. I would actually prefer a simple loop like that, plus another variation that doesn't take a number at all, just keeps going until break is encountered, i.e:

      loop
      {
      // code

      if (condition)
          break;
      

      }

      Then you have a simple construct that you can use for while loops, do-while loops, or anything in-between if you so desire. Just move the position of the condition to where you want to break out.

      V Offline
      V Offline
      Vitaly Tomilov
      wrote on last edited by
      #8

      That's what I suggested just above, with operand repeat, but you also want it without the parameter, which is legitimate :) Hey, somebody needs to quickly update C++ and C# compilers to support the new syntax! :)

      // repeat n times, or until break is encountered:
      repeat(n)
      {
      }

      // repeat indefinitely, or until break is encountered:
      repeat
      {
      }

      However, the second version wouldn't be of the same value, because it is easier to replicate with already existing syntax of:

      while(true)
      {
      }

      but it surely is nicer (more elegant) nonetheless :)

      Let's agree to disagree! Boris the animal Just Boris.

      1 Reply Last reply
      0
      • M Mike Marynowski

        I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:

        int num = 0;

        while (num < 5)
        {
        Console.WriteLine("Number: " + num++);
        }

        or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:

        int num = 0;

        while (true)
        {
        Console.WriteLine("Number: " + num++);

        if (num >= 5) 
        { 
            break; 
        } 
        

        }

        instead of the do-while version:

        int num = 0;

        do
        {
        Console.WriteLine("Number: " + num++);
        } while (num < 5);

        Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #9

        I agree, but I've encountered very few situations (perhaps none) where one would be useful anyway.

        1 Reply Last reply
        0
        • M Mike Marynowski

          I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:

          int num = 0;

          while (num < 5)
          {
          Console.WriteLine("Number: " + num++);
          }

          or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:

          int num = 0;

          while (true)
          {
          Console.WriteLine("Number: " + num++);

          if (num >= 5) 
          { 
              break; 
          } 
          

          }

          instead of the do-while version:

          int num = 0;

          do
          {
          Console.WriteLine("Number: " + num++);
          } while (num < 5);

          Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]

          J Offline
          J Offline
          Johnny J
          wrote on last edited by
          #10

          Both alternative 1 & 3 are perfectly acceptable to me, depending on whether or not I want to check the condition in the beginning or the end of the loop - there can be good reason for one or the other approach. What I DON'T like is alternative 2 - Doing while (true) is to me a wrong use of the loop. True is not a condition and it's never gonna change, hence the risk of creating an eternal loop exists.

          Why can't I be applicable like John? - Me, April 2011
          -----
          Beidh ceol, caint agus craic againn - Seán Bán Breathnach
          -----
          Da mihi sis crustum Etruscum cum omnibus in eo!
          -----
          Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

          M 1 Reply Last reply
          0
          • M Mike Marynowski

            I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:

            int num = 0;

            while (num < 5)
            {
            Console.WriteLine("Number: " + num++);
            }

            or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:

            int num = 0;

            while (true)
            {
            Console.WriteLine("Number: " + num++);

            if (num >= 5) 
            { 
                break; 
            } 
            

            }

            instead of the do-while version:

            int num = 0;

            do
            {
            Console.WriteLine("Number: " + num++);
            } while (num < 5);

            Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]

            R Offline
            R Offline
            Roger Wright
            wrote on last edited by
            #11

            Mike Marynowski wrote:

            That while tacked on the end is, well, awkward.

            And sticking a break in an infinite loop isn't? Yuck! Talk about non-deterministic! X| A do/while is a perfectly nice construct, provided it's used where it's intended. I'd use it a lot more, if someone would invent a keyboard capable of providing a 50kV pulse to the finger of a user who presses the wrong button, despite clear instructions as to which button should be pressed. That's a command I'd want to ensure is executed at least once, then over and over again until the user stops pressing that button. :-D

            Will Rogers never met me.

            M 1 Reply Last reply
            0
            • M Mike Marynowski

              I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:

              int num = 0;

              while (num < 5)
              {
              Console.WriteLine("Number: " + num++);
              }

              or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:

              int num = 0;

              while (true)
              {
              Console.WriteLine("Number: " + num++);

              if (num >= 5) 
              { 
                  break; 
              } 
              

              }

              instead of the do-while version:

              int num = 0;

              do
              {
              Console.WriteLine("Number: " + num++);
              } while (num < 5);

              Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]

              J Offline
              J Offline
              Joan M
              wrote on last edited by
              #12

              bool bContinue = true;
              int num = 0;

              while (bContinue)
              {
              Console.WriteLine("Number: " + num++);
              bContinue = (num < 5);
              }

              The way I usually handle it if not using the 1 (which is the best IMHO) or the 3.

              [www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.

              https://www.robotecnik.com freelance robots, PLC and CNC programmer.

              M R 2 Replies Last reply
              0
              • J Joan M

                bool bContinue = true;
                int num = 0;

                while (bContinue)
                {
                Console.WriteLine("Number: " + num++);
                bContinue = (num < 5);
                }

                The way I usually handle it if not using the 1 (which is the best IMHO) or the 3.

                [www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.

                M Offline
                M Offline
                Mike Marynowski
                wrote on last edited by
                #13

                I like that one. I approve. Definitely going to steal that from you, if you don't mind :)

                J 1 Reply Last reply
                0
                • R Roger Wright

                  Mike Marynowski wrote:

                  That while tacked on the end is, well, awkward.

                  And sticking a break in an infinite loop isn't? Yuck! Talk about non-deterministic! X| A do/while is a perfectly nice construct, provided it's used where it's intended. I'd use it a lot more, if someone would invent a keyboard capable of providing a 50kV pulse to the finger of a user who presses the wrong button, despite clear instructions as to which button should be pressed. That's a command I'd want to ensure is executed at least once, then over and over again until the user stops pressing that button. :-D

                  Will Rogers never met me.

                  M Offline
                  M Offline
                  Mike Marynowski
                  wrote on last edited by
                  #14

                  There are many situations where while(true) is the best way of doing something, particularly if there are many breakout locations in various nested conditional statements. Hence why, if I must avoid an expensive conditional check in the first run, I just adopted that pattern for the purpose. I'm used to seeing breaks at various points in while loops, sometimes "infinite" loops, so I don't really feel the "yuck"-ness of it. Joan Murt has the nicest solution I've seen so far which I'm definitely going to steal though, a couple posts down from yours :)

                  1 Reply Last reply
                  0
                  • J Johnny J

                    Both alternative 1 & 3 are perfectly acceptable to me, depending on whether or not I want to check the condition in the beginning or the end of the loop - there can be good reason for one or the other approach. What I DON'T like is alternative 2 - Doing while (true) is to me a wrong use of the loop. True is not a condition and it's never gonna change, hence the risk of creating an eternal loop exists.

                    Why can't I be applicable like John? - Me, April 2011
                    -----
                    Beidh ceol, caint agus craic againn - Seán Bán Breathnach
                    -----
                    Da mihi sis crustum Etruscum cum omnibus in eo!
                    -----
                    Just because a thing is new don’t mean that it’s better - Will Rogers, September 4, 1932

                    M Offline
                    M Offline
                    Mike Marynowski
                    wrote on last edited by
                    #15

                    I like Joan Murt's way of doing it below, I'm stealing it. But in reply to your post, this is from my reply to another similar post below: There are many situations where while(true) is the best way of doing something, particularly if there are many breakout locations in various nested conditional statements. Hence why, if I must avoid an expensive conditional check in the first run, I just adopted that pattern for the purpose.

                    1 Reply Last reply
                    0
                    • M Mike Marynowski

                      I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:

                      int num = 0;

                      while (num < 5)
                      {
                      Console.WriteLine("Number: " + num++);
                      }

                      or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:

                      int num = 0;

                      while (true)
                      {
                      Console.WriteLine("Number: " + num++);

                      if (num >= 5) 
                      { 
                          break; 
                      } 
                      

                      }

                      instead of the do-while version:

                      int num = 0;

                      do
                      {
                      Console.WriteLine("Number: " + num++);
                      } while (num < 5);

                      Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]

                      R Offline
                      R Offline
                      Rage
                      wrote on last edited by
                      #16

                      1 or 3. Anything else is ugly, even Joan's solution is too verbose for what needs to be done.

                      1 Reply Last reply
                      0
                      • J Joan M

                        bool bContinue = true;
                        int num = 0;

                        while (bContinue)
                        {
                        Console.WriteLine("Number: " + num++);
                        bContinue = (num < 5);
                        }

                        The way I usually handle it if not using the 1 (which is the best IMHO) or the 3.

                        [www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.

                        R Offline
                        R Offline
                        Rage
                        wrote on last edited by
                        #17

                        This is only interesting if you have more than one condition, and avoids having a long test in the while brackets. In the very example you have given, I don't find it better than the solution 1.

                        J 1 Reply Last reply
                        0
                        • M Mike Marynowski

                          I like that one. I approve. Definitely going to steal that from you, if you don't mind :)

                          J Offline
                          J Offline
                          Joan M
                          wrote on last edited by
                          #18

                          I do mind... this is under a patent that I own in all the countries in all the worlds of all the galaxies (known and unknown)... MUUUUWHAHAHAHAHAHAHAA! Please feel free to use it, I'm using this way when several conditions have to be evaluated even in different parts of the loop. ;)

                          [www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.

                          https://www.robotecnik.com freelance robots, PLC and CNC programmer.

                          1 Reply Last reply
                          0
                          • R Rage

                            This is only interesting if you have more than one condition, and avoids having a long test in the while brackets. In the very example you have given, I don't find it better than the solution 1.

                            J Offline
                            J Offline
                            Joan M
                            wrote on last edited by
                            #19

                            Yes, of course, all the options (except 2) are good and it all depends on preferences and/or the situation, typically I go for the one I proposed, but mostly in cases in which there are more than one condition to be handled. If there is only one condition, then I usually use the solution 1.

                            [www.tamautomation.com] Robots, CNC and PLC machines for grinding and polishing.

                            https://www.robotecnik.com freelance robots, PLC and CNC programmer.

                            1 Reply Last reply
                            0
                            • M Mike Marynowski

                              I find myself using regular while loops instead, even if the check will be performed one extra time, i.e:

                              int num = 0;

                              while (num < 5)
                              {
                              Console.WriteLine("Number: " + num++);
                              }

                              or sometimes I'll do this, if I really want to avoid an expensive conditional check the first run through:

                              int num = 0;

                              while (true)
                              {
                              Console.WriteLine("Number: " + num++);

                              if (num >= 5) 
                              { 
                                  break; 
                              } 
                              

                              }

                              instead of the do-while version:

                              int num = 0;

                              do
                              {
                              Console.WriteLine("Number: " + num++);
                              } while (num < 5);

                              Obviously this is an over-simplified example, but you get the idea. I find the alternatives more readable than the do-while loop, which just feels like an awkward construct. That while tacked on the end is, well, awkward. Do you guys use do-while everywhere you know the loop will run at least once, or do you tend to cheat like me because you think do-while's are icky? EDIT (for those just joining in): Joan Murt posted the best solution I've seen so far below: http://www.codeproject.com/Lounge.aspx?msg=4295326#xx4295326xx[^]

                              realJSOPR Offline
                              realJSOPR Offline
                              realJSOP
                              wrote on last edited by
                              #20

                              do/while implies that the code must run at least once, while while by itself checks a condition before running the first time. Each has it's place. I use what's appropriate.

                              ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                              -----
                              You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                              -----
                              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups