• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

plz exaplin me this Prog

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
orginal prog from K&B Book
int i = 0 ;
outer:
while (true) {
i++;
inner:
for (int j=0;j< 10 ;j++){
i+=j;
if(j==3);
continue inner;
break outer;
}
continue outer;
}
System.out.println(i);

---------------------------------------------

modified by me to check the out put but its showing error plz tell me
where i am wrong
-----------------------------------------------
class Tea {
static int i = 0 ;
public static void main(String []args){

outer:
while (true) {
i++;
inner:
for (int j=0;j< 10 ;j++){
i+=j;
if(j==3);
continue inner;
break outer;

}
continue outer;
}
System.out.println(i);

}
}
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

please take a close look into the program especially,

if(j==3);
continue inner;
break outer;

see, the if statement has been terminated with ;. so there is no true block or false block for the if. whether j is equal to 3 or not equal to 3, it will execute the next command
contine inner;
once you issued the continue statement, then the control transfers to the start of that loop. So there is no way to execute
break outer;
due to this only, it is throwing compile time error.

Cheers,
Loga
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is it a problem that 'break outer;' is unreachable?
arno
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its because the statement if(j==3); is equivalent to

if(j==3)
{}
continue inner;

So the compiler says you cant reach the break statement under any conditions. Thats why it throws the error.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code will throw one more compilation error for the line

System.out.println(i);


This is because, this print statement is after a while(true) loop and this loop does not break (since break statement is not reachable by the compiler).
 
A timing clock, fuse wire, high explosives and a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic