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

# Flow Control

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:

Nice people, please kindly explain to me the steps in the below flow control that leads to the answer 0 1 1 1 2 3 3
I'm really beginning to develop negativity towards such questions because I get it right at times and at times not. Please help me understand.
Thank you in advance.

Ranch Hand
Posts: 774
• Number of slices to send:
Optional 'thank-you' note:

Ida Achi wrote:

Well,
First iteration of outer loop

j's value = 0

The inner loop will print 0 then the last condition is met that if j ==0 break from this inner loop, so at this point

0

j increments to 1

Enters inner loop and executes normally for 3 times , as there is no condition met to break from the inner loop
so at this point output would be

0 1 1 1

j increments to 2

The inner loop executes normally for one times, but at the second iteration as this condition is encountered

As compiler sees, value of j was 2 so this condition evaluates to true and before the third iteration, the loop terminates.
At this stage, output is

0 1 1 1 2

outer loop third iteration

j=3

Enters the inner loop and prints out the value of j one time, same on second iteration as well, now at sametime after
printing value of j the second time see the state of variables and condition

j=3 and k= 1

so this condition which is given as

evaluates to true and terminates the outer most loop as well.
At this stage the output is

0 1 1 1 2 3 3

Hope this helps,

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
[note: I didn't see Prithvi's nice reply when I first posted this ...]

Hi Ida,

I think this is one of those problems where nice formatting helps:

When the computer reaches the break at line 10, it exits the "inner" For Loop,
goes to line 4, increments j, and starts the next iteration of the "outer" For Loop
if j < 5. The break at line 8 causes the computer to exit the loop with
the label "foreach", which is the "outer" For Loop. In this case that ends the program.

- Nick

Ida Achi
Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
Thank you. That definitely makes life alot easier for me now. I've burnt it in my brain. I'll also print this out and stick it on my fridge just in case.

Prithvi Sehgal
Ranch Hand
Posts: 774
• Number of slices to send:
Optional 'thank-you' note:

Nick Puketza wrote:
Hi Ida,
I think this is one of those problems where nice formatting helps:

I guess real exam won't give such a nice formatting. More then the formatting you should really concentrate
on the logical flow because may be in real exam you will encounter more bad formatting then this.

Best Regards,

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
I understand flow and execution of this program . My doubt is that how we can use word "foreach" as its not a java keyword. Please help me to understand this.

Ranch Hand
Posts: 808
• Number of slices to send:
Optional 'thank-you' note:

rohan pathak wrote:I understand flow and execution of this program . My doubt is that how we can use word "foreach" as its not a java keyword. Please help me to understand this.

The outermost for loop is a labeled statement. The word "foreach" is the label. Labeling allows us to break in a fashion that wouldn't be possible otherwise. Without the label, the break in the first conditional would merely break from the inner loop, but iterations of the outer loop would still continue.

 PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad: a bit of art, as a gift, the permaculture playing cards https://gardener-gift.com