wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes When and how to apply else in an if Statement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When and how to apply else in an if Statement?" Watch "When and how to apply else in an if Statement?" New topic
Author

When and how to apply else in an if Statement?

Ola Olusegun
Greenhorn

Joined: Jun 13, 2013
Posts: 15
Hello,
I use Netbeans IDE. Below is a line of code i was practicing with and whenever i applied the else statement it was always missing something. For an if statement the else part of the if statement will be executed if the expression is evaluated to a false,right? please check the code below and tell me what to do for an else part to work and give me samples too.I had to replace the else in the code section with if (fishermen <=2); cos it just keeps highlighting red syntax error.thanks.I need your help cos it's good to practice and practice and seek help for better understanding.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7713
    
  20

Ola Olusegun wrote:I use Netbeans IDE.

Well there's your first mistake, in my view. At this stage, I'd strongly advise you to use a decent text editor - Notepad+ works for me - and learn to compile and jar from the command line. However, that's by-the-by.

For an if statement the else part of the if statement will be executed if the expression is evaluated to a false,right?

Right.

please check the code below and tell me what to do for an else part to work...

Well, the main problem is your basic if statement:
if(fishermen <=2);
does absolutely nothing.

And the reason is that terminating ";", which is a null statement.

The format for an if statement is:where "execution_block" =
The braces are only required if you have multiple statements in the block, but I'd advise you to ALWAYS put them in.

and give me samples too.

Erm, actually, why don't you read the tutorials? There are plenty in there, and it'll save my CTS.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nur Suhaylah Ghouse
Greenhorn

Joined: Jul 28, 2013
Posts: 2

basically its...

if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When and how to apply else in an if Statement?