Some problem
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I have a problem getting to understand this code.
I am failing to understand why is this printing
"hello" only twice.
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.
( tags added)
[ November 29, 2004: Message edited by: Barry Gaunt ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
1st Iteration: i=0; j loops from 0 to 9. Maximal value will be 0 + 9 = 9.
It will not enter the if statement and "hello" is printed out.
2nd Iteration: i=1; j loops from 0 to 9. Maximal value will be 1 + 9 = 10.
Same again, "hello" is printed out.
3nd Iteration: i=2; j loops from 0 to 9. Maximal value will be 2 + 9 = 11.
11 > 10. "break lab" is called. This stops all actual loops and returns
to the label "lab" (is a nasty "goto"!!)
All following iterations are entering the if statement and "hello" is never
printed again.
Regards
Michael
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
(for anindam, not Michael
)[ November 29, 2004: Message edited by: Barry Gaunt ]
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 :-).
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
?All following iterations are entering the if statement and "hello" is never
printed again.
There are no following iterations. Execution is complete when break lab is performed because it breaks out of the outer loop.
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
why do you say
There are no following iterations. Execution is complete when break lab is performed because it breaks out of the outer loop.
I thought, a labelled break is working like a "goto". So after calling
"break lab" it breaks the loop and returns to the label.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Michael Imhof:
I thought, a labelled break is working like a "goto". So after calling
"break lab" it breaks the loop and returns to the label.
If you modify the code to check if it goes into the outer loop after teh break lab statement as follows:
You will get the following output:
in lab
hello
in lab
hello
in lab
which shows that the outer loop exits after the labelled break. You are right its 'LIKE' a goto but the difference is that it will goto the label and break the loop that has the label ... I hope that gives it a bit of clarity! :-)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hope this helps,
jdmaddison
| Eat that pie! EAT IT! Now read this tiny ad. READ IT! The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









