• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

continue outer

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems like I never ever got the right answer for this kind of problems, could anyone please explain step by step to me?
30. Consider the code fragment below:
outer: for( int i = 1; i <3; i++ )
{ inner: for( j = 1; j < 3; j++ )
{ if( j==2 )
continue outer;
System.out.println( "i = " +i ", j = " + j );
}
}
Which of the following would be printed to standard output?
a) i = 1, j = 1
b) i = 1, j = 2
c) i = 1, j = 3
d) i = 2, j = 1
e) i = 2, j = 2
f) i = 2, j = 3
g) i = 3, j = 1
h) i = 3, j = 2
Answer: a,d
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI!
1.outer: for( int i = 1; i <3; i++ )
2. { inner: for( j = 1; j < 3; j++ )
3. { if( j==2 )
4. continue outer;
5. System.out.println( "i = " +i ", j = " + j 6. );
7. }
8. }
In the above code, the variable i takes the values:1 and 2. Simmilarly, the variable j takes its values as 1 and 2.
Now acording to the code, when the value of j is equal to 2, the outer loop is continued. So the answers are options a and d, as the starting value of both i and j is 1 and that is printed, then the value of i and j becomes 2, so here the line 4 comes into play as j equals to 2 and the inner loop is abandoned. So there are only two correct answers- a and d.
I hope this helps,
Rachel.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.outer: for( int i = 1; i <3; i++ )<br /> 2. { inner: for( j = 1; j < 3; j++ )<br /> 3. { if( j==2 )<br /> 4. continue outer;<br /> 5. System.out.println( "i = " +i ", j = " + j 6. );<br /> 7. }<br /> 8. }<br /> 9.<br /> here we can go line by line execution of this code<br /> line 1 --> i = 1 as less than 3 goes to line 2<br /> line 2 --> j = 1 as less than 3 goes to line 3
line 3 --> false goes to line 5
line 5 prints 1,1 goes to line 2 again ( here u get he print ans (a))
line 2 --> j = 2 as less than 3 goes to line 3
line 3 --> true goes to line 4
line 4 --> goes to line 1 with continue so the value of i will continue with the next iteration and the inner loop is finished
line 1 --> i = 2 as less than 3 goes to line 2
line 2 --> j = 1 as less than 3 goes to line 3
line 3 --> false goes to line 5
line 5 prints 2,1 goes to line 2 again ( here u get he print ans (d))
line 2 --> j = 2 as less than 3 goes to line 3
line 3 --> true goes to line 4
line 4 --> goes to line 1 with continue so the value of i will continue with the next iteration and the inner loop is finished
line 1 --> i = 3 as its not less than three the loop terminates and comes to line 9
 
lucy hu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rachel and Anil, thank you very much for your help!
Lucy
reply
    Bookmark Topic Watch Topic
  • New Topic