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

Loop sequences

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I was confident in figuring out loops, however, if anyone could give me some clarification as to their pattern, it would be greatly appreciated. Take this sample (MindQ):
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

I think the answer is a, b,..but I always get confused at this point - does i (after continue outer) start back at 1, or does it carry on from where it left off, being 2?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is a,d.
The outer loop will continue iteration(i = 2) on continue statement.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thomas,
The answer given by laksmi is correct. You could have easily verified the answer in a PC.
the statement
if( j==2 )
continue outer;
System.out.println( "i = " +i ", j = " + j );
will never allow the value of j to be printed as 2, Hence j will be always "1" in inner loop. When j reaches "2" the outer loop continues in which i can be 1, 2 only.
hence ans is
i=1 j=1
i=2 j=1
solaiappan

[This message has been edited by P SOLAIAPPAN (edited October 29, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic