Thread: imitations

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    U.S.
    Posts
    1

    imitations

    Hi!

    I'm new to the boards, yet not new to forum posting and programming (have been doing both for almost half a decade), I hope I can have a nice stay here and see how it goes

    I'm currently learning C++ and would like to know something; I know that many of the loops in C/C++ can be represented using a regular while loop. In the following examples, please correct me if I'm wrong:

    Representing a while-statement using a do-statement
    Code:
    do
    {
         if (/* condition */) 
         {
              // ...
         }
         else
              break;
    } while (true);
    Representing a for-statement using a do-statement
    Code:
    int i = 0;
    do
    {
         if (i < MAX)
         {
              // ...
              i++;
         }
         else
              break;
    } while (true);
    Representing a while-statement using a for-statement
    Code:
    for (; /* condition */;)
    {
         // ...
    }
    Representing a do-statement using a for-statement
    Code:
    // the statements go here...
    for (; /* condition */;)
    {
         // same statements as above...
    }
    These are my guesses at the moment. Please feel free to correct me if I happened to make a mistake in any of those or if you see that any of them are inefficient (say, if I included something that was not necessary and there is a better way to represent the same thing, etc.)

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your last one is somewhat incorrect, since do-while ALWAYS executes at least once, so the correct solution would be:
    Code:
    for(;;)
    {
       ...
       if(condition)
          break;
    }
    Of course, if condition is not true before the loop, you won't notice the difference.

    I tend to use "do - while" specifically when I want something done AT LEAST once - e.g. reading and validating data from user - you want to read in the value once at least, then if that's not an OK value, you repeat, until you get a valid value.

    I use while when I have an unknown number of iterations, and sometimes the loop is not needed at all.

    For-loops I choose when there is something that we can somehow "count" across. This is not strictly just incrementing a variable or some such - I'm happy using for-loops to iterate across a linked list for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah a classic case of use for a do ... while comes from manually implementing a function for converting a number to string. You always want at least one digit.

    The first one could be simplified to:
    Code:
    do
    {
         if ( ! /* condition */) 
              break;
         // ...
    } while (true);
    But you could also do it like this:
    Code:
    if (/* condition */) 
         do
         {
              // ...
         } while (condition);
    The assembly that actually gets generated goes as far as doing it like this:
    Code:
    goto loopend;
    do
    {
         // ...
         loopend:
    } while (condition);
    The second one:
    Code:
    // initialisation part
    do
    {
         if (! /* condition */ )
              break;
         // ...
         // next iteration part
    } while (true);
    For loops don't have to count over values. They can for example, walk over a linked list.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    The assembly that actually gets generated goes as far as doing it like this:
    Code:
    goto loopend;
    do
    {
         // ...
         loopend:
    } while (condition);
    Yes, we can probably trust MOST compilers to do that - but not all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed