• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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);
   }
}

 
Marshal
Posts: 13447
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: 64179
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 64179
215
  • 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
Marshal
Posts: 13447
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.
 
Saloon Keeper
Posts: 20657
122
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
Marshal
Posts: 13447
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!