programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Some problem

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
G'day

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.

[ November 29, 2004: Message edited by: Barry Gaunt ]

Greenhorn
Posts: 24
• 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

Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
A question for you: why do you say it prints "hello" for i=0 and j=0?

(for anindam, not Michael )
[ November 29, 2004: Message edited by: Barry Gaunt ]

Ranch Hand
Posts: 47
• 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.

[ 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 :-).

Barry Gaunt
Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
But this question is for Michael: why do you say

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.

Michael Imhof
Greenhorn
Posts: 24
• 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.

Aneesha Singh
Ranch Hand
Posts: 47
• 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! :-)

Ranch Hand
Posts: 47
• Number of slices to send:
Optional 'thank-you' note:
What would happen if we said "break" instead of "break lab"? So I guess the point is that when it does not reach the if statement "hello" prints but the moment if statement is satisfied the execution completes and the "break lab" statement takes you out of both for loops.

Ranch Hand
Posts: 53
• Number of slices to send:
Optional 'thank-you' note:
A break without a label exits the innermost loop. If you want to resume the evaluation of the outer loop, try "continue lab" instead of "break lab".

Hope this helps,