Thread: What C++ loops can provide both zero and 1-trip behavior?

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    What C++ loops can provide both zero and 1-trip behavior?

    I think its a forever loop but can a for loop also do this?
    Pls help.
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: What C++ loops can provide both zero and 1-trip behavior?

    Originally posted by correlcj
    I think its a forever loop but can a for loop also do this?
    Pls help.
    What is it exactly that you are looking for. Do you mean a while loop?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    do/while - posttest always one trip

    while and for- pretest- 0 or more times

    Those are the only three loops in C!
    Mr. C: Author and Instructor

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: Re: What C++ loops can provide both zero and 1-trip behavior?

    Originally posted by MrWizard
    What is it exactly that you are looking for. Do you mean a while loop?
    i would guess he's asking for one trip loops, but maybe thats just me

    mister C has already outlined the answer tho
    hello, internet!

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    quick recap. The 3 types of conditional loop in c/c++ are :-
    TOPTESTED LOOP
    This is a while loop. The loop will continue while the condition is true.
    Code:
    while(1) // will loop forever. same as while(true)
    { // do something}
    BOTTOMTESTED LOOP
    This is a do/while loop. This type of loop always executes once and then the condition is tested and looping continues while condition is true.
    Code:
    do
    {// do something}
    while(condition);
    COUNTED LOOP
    This is a for loop. This type of loop occurs for a counted number of iterations. for instance this will loop 10 times.
    Code:
    for(int i=0;i<10;i++)
    {// do something}
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I think its a forever loop but can a for loop also do this?
    A forever loop.

    Maybe this?

    Code:
    #define EVER ( ;; )
    
    
     forEVER {
    
    
     }
    /*edit*/...damn smilies...
    Last edited by Sebastiani; 10-26-2002 at 12:20 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    in other words...
    What I understood from the 1-trip loop is that you are taliking about the do/while loop.
    do{
    //statements
    } while( condition )
    the statements will be executed at least once even if the condidiotn is false.

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    COUNTED LOOP
    This is a for loop. This type of loop occurs for a counted number of iterations. for instance this will loop 10 times.
    Code:
    for(int i=0;i<10;i++)
    {// do something}
    [/B]
    that's a very poor and inaccurate way to describe a for loop.
    hello, internet!

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    three types? i thought there were only two types:

    Conditional: Do..while / while etc

    Counter: For

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I don't see how for loops are counted and while/do..while loops are conditional. Most while/do..while loops are counted anyway (if not all)

    int i = 0;
    while (i < 10) { /* do something */ };
    for (int i = 0; i < 10; i++) { /* do something */ };
    int i = 0;
    do { i++ /* do something else */ } while (i < 10);

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Eibro
    I don't see how for loops are counted and while/do..while loops are conditional. Most while/do..while loops are counted anyway (if not all)

    int i = 0;
    while (i < 10) { /* do something */ };
    for (int i = 0; i < 10; i++) { /* do something */ };
    int i = 0;
    do { i++ /* do something else */ } while (i < 10);
    All loops can be both counted and conditional, but for-loops are especially made to be counted and while-loops are made to be conditional.
    Making for-loops conditional have no real purpose (it's way easier to use a while loop).

    Examples of non-counted while loops:
    Code:
    bool Looping = true;
    while(Looping)
    {
       ...
    
       if(kbhit()) Looping = false;
    }
    Code:
    while(GetMessage(...))
    {
       TranslateMessage(...);
       DispatchMessage(...);
    }
    Edit:
    If you want an easier counted loop function:
    Code:
    #define LOOP(n) for(int i=0; i<n; i++)
    
    int main()
    {
       LOOP(5) cout << i;
       LOOP(100) 
       {
          cout << "I'm the best!" << endl;
       }
       return 0;
    }
    Last edited by Magos; 10-26-2002 at 10:21 AM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Registered User tgm's Avatar
    Join Date
    Jun 2002
    Posts
    150
    Zero and 1 trip ability sounds like an 'if' statement to me, not a loop.

  13. #13
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Thanks for all your posts but...

    What C++ loops can provide both zero and 1-trip behavior
    That is the question posted to me in the book. Its asking for one or more than one loop(ie. while, do/while, forever or for) these can only be the answers. I can only assume that it must be them all and is a trick question. I am still

    I understand that there are 3 types of loops really but c++ provides 4 different loops.
    1. while
    2. do
    3. for
    and forever which is a simpified for loop eg;
    Code:
    for ( ; ;)
    {
        statement1;
            if (condition)    break;
                statement2;
    }
    but actually there are 3.
    Thanks for the posts, i am going to say all of them and see how that flies. weird question?
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  14. #14
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Its FOREVER!

    The correct answer is the forever loop because it eliminates redundant coding and it can be used in both times where statements must be executed 0 or more times and other statements must be executed 1 or more.
    THANKS! for everyones input!!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  15. #15
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You should try to only use the for loop and the while
    loop. Try to never use infinite loops, do while, goto,
    break(except in switch statements) or continue.

    This is because you
    reason somewhat about the correctness. So you have
    Code:
    while(<condition>)
    {
           <condition> = true inside the loop
    
    }
    <condition> = false
    
    for(<stmt_op>; <condition>; <stmt_op>)
    {
             <condition> = true inside the loop
    }
    <condition>  = false

Popular pages Recent additions subscribe to a feed