• 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
  • 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

Marcus Green Mock #1 Q 21

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could someone give an explanation(walkthrough) of what is going on here. I'm not quite sure how to follow it.
________________________________________________________________
What will be output by the following code?
public class MyFor{
public static void main(String argv[]){
int i;
int j;
outer:
for (i=1;i <3;i++)
inner:
for(j=1; j<3; j++) {
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
}
}
}
1) Value for i=1 value for j=1
2) Value for i=2 value for j=1
3) Value for i=2 value for j=2
4) Value for i=3 value for j=1
_______________________________________________________________
The answer is 1) and 2) . I compiled and ran it myself and it's true but i'm still not sure what's going on.
Thanks Very Much,
Tom
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Tom
Hope I can explain this clearly , let me try:
1) the for loop indicated by outer: starts, value of i=1; since i < 3 , we fall into the inner: for loop, where j = 1. Since the condition j==2 is not true we print i=1,j=1
2) Now the inner loop continues, j is now 2 . So the statement continue outer is executed.
3) remember that in the outer loop , i is now 2, since 2 < 3 we get into the inner loop again where now j=1. Since j==2 is not true we print i=2, j=1.
4) we now continue in the inner loop, j is now 2. j==2 is now true so we execute continue outer
5) Remember that in the outer i=3, i<3 - false, so we exit the outer for loop. Thus the execution ends and you get the answers
i=1,j=1
i=2,j=1.
Hope this is clear.
 
Tom Graziose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Latha !
Check this out. I modified the code and placed some displays to stdout. Here's the code followed by the output.
public class MyFor{
public static void main(String argv[]){
int i;
int j = 0;
outer:
for (i=1;i <3;i++)
{
System.out.println("in outer " + "i=" + i + " j=" + j);
inner:
for(j=1; j<3; j++) {
System.out.println("in inner " + "i=" + i + " j=" +j);
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
}
}
}
}
/*
OUTPUT:
in outer i=1 j=0
in inner i=1 j=1
Value for i=1 Value for j=1
in inner i=1 j=2
in outer i=2 j=2
in inner i=2 j=1
Value for i=2 Value for j=1
in inner i=2 j=2

 
reply
    Bookmark Topic Watch Topic
  • New Topic