Monday, August 24, 2020

Java if-else statement

The Java if statement is used to test the condition. It checks boolean condition: true or false.

There are various types of if statement in Java…

·        if statement

·        if-else statement

·        if-else-if ladder

·        nested if statement


if Statement

Use if to specify a block of code to be executed, if a specified condition is true.

Syntax:

if (condition) {
//code to be executed
}

 Flow Chart

      



//Java Program to demonstrate the use of if statement
public class IfExample {
     public static void main(String[ ] args) {
           //defining a ‘gpa’ variable
           double gpa = 9.5;
           //checking gpa
           if(gpa>7.5) {
                System.out.print(“gpa is greater than 7.5”);
           }
     }
}

Note: if is in lowercase letters. Uppercase letters (IF or If) will generate an error.

Output

gpa is greater than 7.5


if-else Statement

  • Use  if to specify a block of code to be executed, if a specified condition is true.
  • Use else to specify a block of code to be executed, if the same condition is false.

Syntax:

if (condition) {
//code to be executed
}
else {
//code to be executed
}

 Flow chart


 


Example

//A Java Program to demonstrate the use of if-else statement
//It is a Program of odd and even number
public class IfElseExample {
     public static void main(String[ ] args) {
         //defining a variable
         int n=6;
         //Check the number n is Odd or Even
         //n is even if it is divisible by 2
           if(n%2==0)
                System.out.println(n + “ is an even number”);
           else
                System.out.println(n + “ is a odd number”);
     }
}

Output

6 is an even number


Leap Year Example

A year is a leap, if it is divisible by 4 and 400. But not by 100.

public class LeapYearExample {
     public static void main(String[ ] args) {
           //defining a variable
           int year=2020;
           if((year % 4 == 0)&&(year % 100 != 0))||( year % 400 == 0)
                System.out.println(year + “ is a leap year”);
           else
                System.out.println(year + “ is not a leap year”);
     }
}

Output

2020 is a leap year

Using Ternary Operator

We can also use ternary operator (? :) to perform the task of if-else statement.

It is a short hand way to check the condition.

If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned.

Example

public class Operator {
     public static void main(String[ ] args) {
           double number= -9.52;
           String result;
           result = (number > 0.0) ? “positive” : “not positive”;
                System.out.println(number + “ is ” + result);
     }
}

Output

-9.52 is not positive


if-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

if (condition1) {
//code to be executed if condition1 is true
}
else if (condition2) {
//code to be executed if condition2 is true
}
Else if (condition3) {
//code to be executed if condition3 is true
}
…..
else {
//code to be executed if all the conditions are false
}

 Flow Chart 

 

  

Example

//Java Program to demonstrate the use of if-else-if ladder statement
//It is a Program of grading system for A+, A, B, c, D, E grade and Fail
public class IfElseExample {
     public static void main(String[ ] args) {
           int marks=95;
           if(marks>=90 && marks<100)
                System.out.println(“A+ grade”);
 
           else if(marks>=80 && marks<90)
                System.out.println(“A grade”);
 
           else if(marks>=70 && marks<80)
                System.out.println(“B grade”);
 
           else if(marks>=60 && marks<70)
                System.out.println(“C grade”);
 
           else if(marks>=50 && marks<60)
                System.out.println(“D grade”);
 
           else if(marks>=34 && marks<50)
                System.out.println(“E grade”);
 
           else if(marks<34)
                System.out.println(“Fail”);
 
           else
                System.out.println(“Invalid”);
     }
}   

Output

A+ grade


Program to check Positive, Negative or Zero

public class PositiveNegativeExample {
     public static void main(String[ ] args) {
           int number=-6;
           if(number>0)
                System.out.println(“Positive”);
 
           else if(number<0)
                System.out.println(“Negative”);
 
           else
                System.out.println(“Zero”);
     }
}

Output

Negative

 

Nested if statement

The nested if statement represents the if block within another if block.

Here, the inner if block condition executes only when outer if block condition is true.

Syntax:

if (condition) {
      //code to be executed
    if (condition) {
                 //code to be executed
    }

 Flow Chart


  

                          


//A Java Program to demonstrate the use of if-else statement
public class NestedIfExample {
  public static void main(String[ ] args) {
   //Creating two variable for age and weight
   int age=19;
   int weight=56;
   //applying condition on age and weight
   if(age>=18) {
    if(weight>=50)
   System.out.println(“You are eligible to donate blood”);
   else
   System.out.println(“You are not eligible to donate blood”);
    }
   else
   System.out.println(“Age must be greater than 18”);
    }
}

Output

You are eligible to donate blood

3 comments: