• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Stuck on looping/flow control example

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to determine when the variables are incremented in the following example....I'm stuck on this one.
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 3; y++) {
if (x == y) {
continue;
}
System.out.println("x = " + x + " y = " + y);
}
}
Any help that would explain how this example works and when the variables are incremented will be a big help since I'm studying for SCJP.
Thanks in advance.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike...
Whenever x==y there wont be printout
So that means only
01
02
10
12
will be printed out
Whenever you see continue, immediately go back to the loop (incrementer ie i++) again...(as a human compiler i mean )

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how it works.
1.first time x=0 and y=0,so it checks condition x==y,it is true so it continues with the inner loop incrementing y.
2.now x=0,y=1,now the test case is false and it prints out the value of x and y i.e 0 and 1.
3.Again y is incremented,so x=0 and y=2,test case is false and prints the value of x=0 and y=2.
4.Now the inner loop is completed as test condition is y<3,so now it continues with the outer loop so now x=1 and y=0 and it prints this value.
5.now the inner y loop is incremented i.e for x=1 and y=1 ,test case is true so it continues with the inner loop.
6.now x=1,y=2 and it prints the value.so now both inner and outer loop are completed.
7.the valued printed are x=0,y=1
x=0,y=2
x=1,y=0
x=1,y=2
 
Mike Cunningham
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the 'y' variable get reset to '0' when it completes the first cycle of the inner loop?
Also, doesn't the increment value at the end of a for statement take place before the code is processed inside the loop? It seems like the first time through the x & y values should be '1'?
 
Anu Kris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does the 'y' variable get reset to '0' when it completes the first cycle of the inner loop?
Also, doesn't the increment value at the end of a for statement take place before the code is processed inside the loop? It seems like the first time through the x & y values should be '1'?
==========
y gets reset to 0 every time it enters the loop again.
also for every initial value of the variable the body of the loop gets executed.hope this helps
 
Mike Cunningham
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's exactly what I was looking for. Thank you!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic