• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Cannot understand the flow of code...

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TestClass
{
public static void main(String args[])
{
int x = 0;
labelA: for (int i=10; i<0; i--)
{
int j = 0;
labelB:
while (j < 10)
{
if (j > i) break labelB;
if (i == j)
{
x++;
continue labelA;
}
j++;
}
x--;
}
System.out.println(x);
}
}
Options:
1)It will not compile
2)It will go infinite loop when run
3)The program will write 10 to the standard output
4)The program will write 0 to the standard output
5)None of the above..
Answer: 4)It will write 0 to the standard output.
I tried running the program and got the output 0
Can any one explain me the flow control in this case?
Sonir
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for loop condition will never be true.
So, the loop will be skipped and the value of
x, 0, will be printed to the screen.
 
reply
    Bookmark Topic Watch Topic
  • New Topic