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;
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;
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
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
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):
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.Code:for (int i = 0; i < 16; i++) cout << "done" << endl;
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.
You usually see code like that on exams. Testing your ability to read syntax.
What the hell is wrong with people? Why don't you compile it and find out for yourself?Originally Posted by ameber
Quzah.
Hope is the first step on the road to disappointment.
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 watchingOriginally Posted by quzah
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.
you are right! I'm sleeping instead of working hard!