Win a copy of Pipeline as Code this week in the Cloud/Virtualization 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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.
 
Marshal
Posts: 67514
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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..
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic