aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about results of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about results of code" Watch "Question about results of code" New topic
Author

Question about results of code

Riaz ahmed pinjari
Greenhorn

Joined: Mar 04, 2013
Posts: 2

Hi Meena,
in the below code

public class Circles {
public static void main(String[] args) {
int[] ia = {1,3,5,7,9};
for(int x : ia) {
for(int j = 0; j < 3; j++) {
if(x > 4 && x < 8) continue;
System.out.print(" " + x);
if(j == 1) break;
continue; }
continue;
}
}
}

Why is the answer 113399 and not 139?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39


This topic has been split off from here...

http://www.coderanch.com/t/528730/java-programmer-SCJP/certification/kathy-sierra-Flow-Control

And is assumed to be a K&B question.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39

Riaz ahmed pinjari wrote:Hi Meena,
in the below code



Why is the answer 113399 and not 139?


Follow one of the iterations of the outer loop (either 1, 3, or 9), and see what happens. What does the inner loop do during this one iteration of the outer loop?

Henry
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 592

for x =1, for j = 0, print 1 and then continue to j=1, print 1, but j= 1 , break the inner loop, continue the outer loop.Output so far : 1 1
for x = 3, for j =0 , print 3, continue to j =1, print 3, break inner loop, continue outer loop. Output so far 1 1 3 3
for x = 5, for j = 0, continue inner loop.for x =5, for j =1, continue inner loop. For j =2 , continue inner loop.
for x=7, for j =0 , continue inner loop. For x = 7, for j=1, continue inner loop. For j =2 , continue inner loop.
for y=9, for j=0, print 9, continue inner loop. For y =9, j=1, print 9 , break inner loop and continue outer loop. But the outer loop comes to the end.
Output 1 1 3 3 9 9




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about results of code
 
Similar Threads
loop
Puzzled with the output of this programme
Forbidden Conversions
Doubt in assertion
Doubt in break and continue statements