Ugly loop!

This is a discussion on Ugly loop! within the C++ Programming forums, part of the General Programming Boards category; Hi every body! Can you tell me what this strange code do? for (int i = 0; i < 4; ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    11

    Ugly loop!

    Hi every body!
    Can you tell me what this strange code do?

    for (int i = 0; i < 4; i++)
    for (int i = 0; i < 2; i++)
    for (int i = 0; i < 2; i++)
    cout <<"Done!" <<endl;

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    It outputs "Done!" 16 times while iterating through some loops. It might be more clear if we indent:
    Code:
    for (int i = 0; i < 4; i++)
       for (int i = 0; i < 2; i++)
          for (int i = 0; i < 2; i++)
             cout <<"Done!" <<endl;
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!
    Done!

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You'd get the same result from using one loop (though I imagine that the code you gave was originally intended as a demonstration of some loop principles):
    Code:
    for (int i = 0; i < 16; i++)
    	cout << "done" << endl;
    When posting code, be sure to use code tags. Place [ code ] at the beginning of your code, and [ / code ] at the end (without the spaces), and it will format the code so it looks better on the board. It puts it in a more appropriate font and preserves indentation.

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    As said...it's just a REALLY ugly and confusing way to print "done". You'd never need to nest for loops to do such a simple task.

    ...And it's usually a good idea to enclose your for, if, and else statements in braces {}.

    Doing so will illustrate much more clearly where each statement is being applied to.

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    You usually see code like that on exams. Testing your ability to read syntax.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ameber
    Hi every body!
    Can you tell me what this strange code do?

    for (int i = 0; i < 4; i++)
    for (int i = 0; i < 2; i++)
    for (int i = 0; i < 2; i++)
    cout <<"Done!" <<endl;
    What the hell is wrong with people? Why don't you compile it and find out for yourself?

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by quzah
    What the hell is wrong with people? Why don't you compile it and find out for yourself?
    Probably because he's sitting in his computer lab taking an exam and was lookin for a quick answer, so he fired up IE while the instructor wasn't watching

  9. #9
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Yeah.... a "What does this do?" isn't the brightest question to ask.

    Anyway Ameber, you could say that the nested loops work from the "inside, out."(At least, that's a good way to visualize it) The inner-most loop executes until it's done, then the outer for loops will execute. This will continue until the outer-most loop's condition statement is false.

    4*2*2 = 16 executions.

    Compile it yourself next time.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    11

    right!!

    you are right! I'm sleeping instead of working hard!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21