Java If Else with Examples and Programs

Java if statement: The if statement is decision-making statement and is used to control the flow of executions of statements. It is the two-way decision statement and is used in conjunction with a Test expression.


Declaration of if statement:
if(test expression)

Explanation: In the above declaration the if statement allows the computer to evaluate the expression first and then depending on whether the value of the expression (means condition) is “True” or “False” it transfers the control to next statement. The program has to follow two paths one for the true condition another for the false condition.

SIMPLE IF STATEMENT
If (test expression)
          {
            statement-block;
           }
           statement-n;

The statement block may be a single statement or a group of statements. If the test expression is true, the statement block will be executed otherwise the statement-block will be skipped and the execution will jump to the statement-n;
Java If Else program Example using Integers:

class IfExample
 {
  public static void main(String[] args)
  {
    int i = 6, j = 7;
    if(i<j)
    {
 System.out.println("J is Greater then I");
    }
  }
}

Output: 
J is Greater then I

Example 2:
class IfExample1
{
public static void main(String[] args)
{
 if(8>6)
 {
 System.out.println(" Welcome to TutorialsWeb ");
 }
}
}

Output:
Welcome to TutorialsWeb

else: It is a keyword using this keyword we can create the alternate block for “if” condition using “else” means always optional, it is recommended to use. When we have an alternate block of “if” condition. When we are working with “if” and “else” only one block will be executed and when the if a condition is false then only the else part will be executed.

if …else Statement:
The if …else statement is an extension of the simple if statement. The Syntax of if …else statement is below:


if(test expression)
{
 True-block statement(s);
}
else
{
 False-block statement(s);
}
statement(x);

Explanation: If the Test expression is true, then the true-block statement(s), immediately following the if statements are executed. Or the False-block Statements are executed. In another case, either true-block (or) False-block will be executed, not both. Then the control is transferred subsequently to the statement-x;

Java If Else program:
import java.util. *;
class IfElseExample
{
public static void main(String[] args)
{
 Scanner s1 = new Scanner(System.in);
 System.out.println("Enter I value");
 int i = s1.nextInt();
 System.out.println("Enter J value");
 int j = s1.nextInt();
 if(i > j)
 {
 System.out.println("I is Greater then J");
 }
 else
 {
 System.out.println("J is Greater then I");
 }
System.out.println("Out Of If Else Loop");
}
}

Output:
Enter I value
15
Enter J value
14
I is Greater then J
Out Of If Else Loop

Explanation:
1)    Two integers “I” and “J” are declared. Input values are received for both “I” and “J” using Scanner.
2)     In the above program if else condition statement is used to check whether “I” is greater than “J”.
3)    If “I” is greater than “J”, the Print message “I is Greater then J” is Printed.
4)    If “J” is greater than “I”, the else part will be executed. The Print message “J is Greater then I” is Printed.
5)    After completing the else part the statement-x will be executed.

Nested If…else Statements in Java: When a series of decision is involved then we have to use more than one if…else statements in the nested form shown below.

Explanation: In the above logic of execution if the Test condition-1 is True, it continues to the Test condition-2. If the Test condition-2 is True, the statement-1 will be evaluated other-wise else statement-2 evaluated. If the condition-1 is false, the statement-3 will be executed, and then the control is transferred to the statement-x;


Program:                                                         
import java.util.*;
class NestedIfelseExample
{
 public static void main(String[] args)
 {
 Scanner s1 = new Scanner(System.in);
 System.out.println("Enter the Number");
 int n = s1.nextInt();
 if(n > 0)
 {
 if(n == 0)
 {
  System.out.println("It is Zero");
 }
 else
 {
 System.out.println("Number is greater than Zero");
 }
 }
 else
 {
 System.out.println("Number is less than Zero");
 }
 }
}

Output:
Enter the Number
12
Number is greater than Zero


The Else If Ladder: Another way of putting ifs together when multipath decisions are involved. A multipath decision of ifs in which the statement associated with each else is an if.  The syntax of Else If Ladder is shown below:
Explanation: In the above Else If Ladder the conditions are evaluated from the top of the ladder to downwards. When the condition found to be True, the statement associated with it is executed and the control is transferred to the statement-x skipping the rest of the ladder. When all the n conditions become False, then the final else with default statement will be executed.

Program:

import java.util.*;
class ElseIfLadderExample
{
 public static void main(String[] args)
 {
 Scanner s1 = new Scanner(System.in);
 System.out.println("Enter the Marks");
 int m = s1.nextInt();
 if(m<50)
    { 
     System.out.println("fail"); 
    } 
    else if(m >= 50 && m < 60)
    { 
     System.out.println("D grade"); 
    } 
    else if(m >= 60 && m < 70)
    { 
     System.out.println("C grade"); 
    } 
    else if(m >=70 && m < 80)
    { 
     System.out.println("B grade"); 
    } 
    else if(m >= 80 && m < 90)
    { 
     System.out.println("A grade"); 
    }
    else if(m >=90 && m < 100)
    { 
     System.out.println("A+ grade");  
    }
    else
    { 
     System.out.println("Invalid!"); 
    } 
  }
}

Output:
Enter the Marks
66
C grade
Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...