CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Loop Imitations

  1. #1
    Join Date
    Apr 2009
    Posts
    21

    Loop Imitations

    Hi!

    As some of you may know, 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
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: Loop Imitations

    Of course, any of these loops can be done using just 'if' and 'goto'. We have all these different loop structures because it makes it a little bit easier on the programmer
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  3. #3
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Loop Imitations

    I think the most common loop that I use is
    Code:
    while(true){
       ...break;
    }
    and

    Code:
    for(unsigned int = 0; i < whatever; i++){
    
    }
    I really don't think there is a difference in performance, I think by the time they hit the abstract parse tree, they are the same. The only limitation that I can think of is that for loops and such are limited by the type that you use as the iterator.

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Loop Imitations

    Quote Originally Posted by EsX_Raptor View Post
    Representing a do-statement using a for-statement
    Code:
    // the statements go here...
    for (; /* condition */;)
    {
         // same statements as above...
    }
    This way you have to write the same code twice. You can also write a do-loop as a for-loop and keep the body the same.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Loop Imitations

    Quote Originally Posted by ninja9578 View Post
    I think the most common loop that I use is
    Code:
    while(true){
       ...break;
    }
    and

    Code:
    for(unsigned int = 0; i < whatever; i++){
    
    }
    I really don't think there is a difference in performance, I think by the time they hit the abstract parse tree, they are the same. The only limitation that I can think of is that for loops and such are limited by the type that you use as the iterator.
    while(true) isn't a really good habit to get into. It's better practice to put a condition in the while statement rather than rely on a break somewhere in the body.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Loop Imitations

    I know, I do that whenever I can, but I've found myself recently having multiple situations that can break the loop, or I have code that has to be placed at the end of the loop that I don't want to run. Don't worry, I stress test it and make sure that there is no way for it to run infinately.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Loop Imitations

    Quote Originally Posted by GCDEF
    while(true) isn't a really good habit to get into. It's better practice to put a condition in the while statement rather than rely on a break somewhere in the body.
    Generally, I agree (though I prefer for (;;) ), but in some cases such a refactoring (particularly to convert to a do while loop) requires pulling out code into a function, which may be overkill.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured