Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When and how to apply else in an if Statement?

 
Ola Olusegun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nur Suhaylah Ghouse
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 981
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic