Originally posted by anindam bhattacharaya:
G'day
I have a problem getting to understand this code.
I am failing to understand why is this printing
"hello" only twice.
( tags added)
[ November 29, 2004: Message edited by: Barry Gaunt ]
The first time the code runs fine, goes through the inner for loop 10 times and never reaches inside the if condition (because i+j is always less than 10).
On coming out of the inner loop it prints hello.
Then i is incremented to 1 AND the above repeats.
Then i is incremented to 2, and in the last iteration of the inner loop it enters the if condition because i+j becomes greater than 10. It goes to statement 'break lab'. lab is the label for the outer loop and the program execution comes out of the outer loop (labelled lab) and the program finishes. Thats the reason hello is printed just once.
in the 1st iteration, i=0,j=0 and i+j not > than 10. print hello
in the 2nd iteration, i=0,j=1 and i+j not > than 10. print hello
in the 3rd iteration, i=0,j=2 and i+j not > than 10. print hello
etc.
Thats because hello is printed in the outer loop after the inner loop is executed. Its not inside teh inner loop. Please note where you have placed your brackets.
Hope that helps :-).