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

Labelled continue

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
What will be the output when you compile and execute the following program.
public class Base{

private void test() {

one:
for (int i = 0; i < 3; i ++) {

two:
for(int j = 0; j < 3; j++){

if (i == 2)
continue two;

if ( j == 2)
continue one;

System.out.println("Values : " + i + "," + j);
}
}
}

static public void main(String[] a) {
new Base().test();
}

}
Select all valid answers
a) Values : 0,0
b) Values : 0,1
c) Values : 1,0
d) Values : 1,1
e) Values : 2,0
f) Values : 2,1
Here the correct answers given are a,b,c,d
According to me f should also be the correct answer
as when i == 2 then there is continue two:
so j=0 iteration should not be executed but j=1 should be executed.so 2,1 should also come .
but i ran this code and f) answer is not coming.
Can somebody enlighten me on this??
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anonymous (?),

Let's trace the program:
Enter loop one
When i = 0,
Enter loop two.
When j = 0, both condition one and two are false. So, print i = 0 and j = 0.
When j = 1, both condition one and two are false. So, print i = 0, and j = 1.
When j = 2, condition one is false, condtion two is true. So, go back to loop one, without printing the values.
When i = 1
Enter loop two.
When j = 0, both condtion one and two are false. So, print i = 1 and j = 0.
When j = 1, both condtion one and two are false. So, print i = 1 and j = 1.
When j = 2, condtion one is false, condtion two is true. So, go back to loop one, without printing the values.
When i = 2,
Enter loop two.
Throughout the loop i will be two and hence condtion one will evaluate to true all the time and hence, it won't come the printing statement.
So, the output will be
0,0
0,1
1,0
1,1.
So, a,b,c and d are the correct answers.
Regards,
Suresh.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic