• 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

If statement with semicolon

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
boolean foo = true;
if (foo);
{
print("hello")
}
if(!foo);
{
print("goodbye");
}

The above program printing both hello and goodbye.
Can anyone explain whats going on here and what is the use of if();
 
Rancher
Posts: 618
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The semicolon ends the if statement. So the next statement is the print("hello") or print("goodbye"); To see this, try changing the line if(!foo); to else (to create an if-else statement).
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You stated and ended condition by saying

if (foo); After that you have a block of code

Again, you started and closed condition by saying
if(!foo); After that you have a block of code
Here is program from my eclipse

From opening to closing braces, It is a block of code. (Code block)
 
naniigadu pokiri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your responses Hari and tom, but what is the use of it, if it always print the statements below to it.
 
Sheriff
Posts: 67749
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not useful. Don't do it.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naniigadu pokiri wrote:thanks for your responses Hari and tom, but what is the use of it, if it always print the statements below to it.

The general rule is semicolon(;) completes the statement. you started if loop and closed it by using semicolon..These is no special use together (i.e., condition and semicolon)(if(true);)
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naniigadu pokiri wrote:thanks for your responses Hari and tom, but what is the use of it, if it always print the statements below to it.


There is no relation between the if statement and the followed code block. Both are different! have a look here~!
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naniigadu pokiri wrote:
but what is the use of it, if it always print the statements below to it.



There's no use of it as such.
It's given in the question just to find out whether one has understood conditional statements in Java.
 
naniigadu pokiri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your responses..
 
Screaming fools! It's nothing more than 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