File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 172
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: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
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: 172
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: 172
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 .
 
Don't get me started about those stupid light bulbs.
 
subject: Flow Control ( Continue & Break )
 
Similar Threads
Dans mock test on control flow
Puzzled with the output of this programme
Question about results of code
Doubt in break and continue statements
Problem with self test question