This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Unreachable statements

 
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: 13556
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 65057
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 65057
247
  • 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: 13556
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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: 20933
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • 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: 13556
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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!