• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with for loop

 
neelam samnani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A mock test which is cretaed by Marcus Green had asked
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);
}
}

According to me answer will be "Value for i =1
Value for j=1 " , but he had answered "Value for i =1 value for j=1 And Value for i =2 , Value for j=1".

I m unable to understand that how can be value of j is remain unchanged while i's value is increasing.
I am not getting it, how it is working?
Pls. anyone can suggest me I 'll be very gratefull to them.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi neelam,
Basically here's what happens

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
neelam samnani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane Griscti to helping me in such a good way.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!