This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Unreachable statements  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the program if I add opening and closing curly braces in if how x++ and other statements becomes unreachable.?

public class Main {

   public static void main(String[] args) {
       int x = 0;
       do {
           x++;
           System.out.println(x);
           if(++x<5)
               continue;
               x++;
               System.out.println(x);

       } while (x++ < 10);
   }
}

 
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

If you meant this:

then any statement after continue will be unreacheable because execution will go directly to the while statement.

Despite your indentation, the if-statement you wrote without braces only has the continue statement in its body. This means that the code will always execute the statements after continue.

When posting code, UseCodeTags (<--click that, it's a link)
 
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 64473
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always tell us where such code comes from, so we can reduce copyright problems, assess the source ourselves, and in this case avoid the source because that is dreadful code which would get you sacked within the hour if you wrote it at work
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please choose a meaningful subject when starting a new thread. "My question" is not meaningful. I have edited the subject to make it more relevant.
 
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Welcome to the Ranch!

If you meant this:
...



Classic example of why I'm so insistent that one always use braces on conditional/loop clauses. "If".
 
prak Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Junilu Lacar wrote:Welcome to the Ranch!

If you meant this:
...



Classic example of why I'm so insistent that one always use braces on conditional/loop clauses. "If".



Thanks a lot.. Tim Holloway
 
prak Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Welcome to the Ranch!

If you meant this:

then any statement after continue will be unreacheable because execution will go directly to the while statement.

Despite your indentation, the if-statement you wrote without braces only has the continue statement in its body. This means that the code will always execute the statements after continue.

When posting code, UseCodeTags (<--click that, it's a link)




Thanks a lot..
 
prak Gupta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Campbell Ritchie]. . . and welcome to the Ranch [/quote]

thank you...[color=darkred] [/color][size=12] [/size]
 
Junilu Lacar
Sheriff
Posts: 13453
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@prak Gupta, please don't quote an entire reply, especially if all you want to do is say "Thank you." A simple "Thank you, everyone" would have sufficed.

... and you're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!