• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

OCA Book Practice questions

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could someone explain me this question clearly for me, Thanks.


for(int i=0;i<5;i++){
System.out.print(1);
if(i<2) continue;
else break;
}


OCA Sybex book, page number,91 says(Table 2.5), that if statement won't allow continue statement.
But the above code compiles without any issue.

I did see some examples in the book with If statement having continue, but with optional labels. so I understood that if statement allows "continue",  only when the optional labels were used.
But I am confused now, could someone explain to me clearly, where I am going wrong.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also find this part in the table confusing. I just assume that this is an error in the book.
 
author & internet detective
Posts: 39525
771
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya/Erick:
Welcome to CodeRanch!  I'm glad you asked because this is an important detail.

This code compiles because the continue is inside of a loop.



It prints the following which shows the continue applies to the loop ("after if" is skipped)


By contrast, this does not compile as there is no loop and if statements are not allowed to use "continue" (per table 2.5)
 
R Karthik
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation.
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!