Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Flow Control ( Continue & Break ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Flow Control ( Continue & Break )" Watch "Flow Control ( Continue & Break )" New topic
Author

Flow Control ( Continue & Break )

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
I am not able to get the following code . Its correct answer is " 1 1 3 3 9 9 " .
I know the break statement terminates the on going for loop and shifts the flow to the first statement after the for loop. & continue statement shifts the flow to the next iteration.

Thanks !!!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14280
    
  21

Tarun Oohri wrote:I know the break statement terminates the on going for loop and shifts the flow to the first statement after the for loop. & continue statement shifts the flow to the next iteration.

That's correct. Now, go through the program line by line in your head, and remember, or write down on a piece of paper, what the value of the variables x and j are at each step. You'll see how the program goes.

You could also run it in a debugger, for example in Eclipse. Set a breakpoint at the beginning of the program and step through the program line by line as it runs.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
Tarun Oohri wrote:I am not able to get the following code . Its correct answer is " 1 1 3 3 9 9 " .
I know the break statement terminates the on going for loop and shifts the flow to the first statement after the for loop. & continue statement shifts the flow to the next iteration.
Also, if we change " For Each " loop with Basic For Loop...then answer changes to " 1133557799 ".
That it # Contradictory #

Thanks !!!
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
Jesper de Jong wrote:
Tarun Oohri wrote:I know the break statement terminates the on going for loop and shifts the flow to the first statement after the for loop. & continue statement shifts the flow to the next iteration.

That's correct. Now, go through the program line by line in your head, and remember, or write down on a piece of paper, what the value of the variables x and j are at each step. You'll see how the program goes.

You could also run it in a debugger, for example in Eclipse. Set a breakpoint at the beginning of the program and step through the program line by line as it runs.


Thanks that is a good idea
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 176
Tarun Oohri wrote:
Jesper de Jong wrote:
Tarun Oohri wrote:I know the break statement terminates the on going for loop and shifts the flow to the first statement after the for loop. & continue statement shifts the flow to the next iteration.


Set a breakpoint at the beginning of the program and step through the program line by line as it runs.


Thanks that is a good idea


Thanks i got it...also got the concept between for & for-each .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Flow Control ( Continue & Break )