Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » FundamentalRSS Feeds

IF-Else Control Statement

Posted By: Bama Cohen     Category: Java     Views: 2488

This article explains about control statement IF-Else in java with examples.

You can add an else statement to the if. Addition of the else provides a two-way decision path and syntax or form of it is as below.

Syntax of IF-Else Statement

if(expression)
statement1;
else
statement2;

If the expression is true, if block will be executed and else block will be skipped. If the expression is false, else block will be executed and if block will be skipped. 

Examples of IF-Else Statement

Example 1 : Program to compare 2 numbers and display greatest
class IfStateDemo
{
  public static void main(String args[])
  {
int num1 = 10;
int num2 = 15;
if(num1 > num2)
{
System.out.println("Number1 having value " + num1 + " is greater than number2 having value " + num2);
}
else
{
System.out.println("Number2 having value " + num2 + " is greater than number1 having value " + num1);
}
  }
}

Output
Number2 having value 15 is greater than number1 having value 10


  
Share: 

 
 

Didn't find what you were looking for? Find more on IF-Else Control Statement Or get search suggestion and latest updates.

Bama Cohen
Bama Cohen author of IF-Else Control Statement is from Jerusalem, Israel.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!