Header Ads

Hii,I am Anand Nayak.This blogger is make for specially basic programs of some language.

If-else Statement

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 :
   Let's see the example of if statement :
     
    
  1. //Java Program to demonstate the use of if statement.  
  2. public class IfExample {  
  3. public static void main(String[] args) {  
  4.     //defining an 'age' variable  
  5.     int age=20;  
  6.     //checking the age  
  7.     if(age>18){  
  8.         System.out.print("Age is greater than 18");  
  9.     }  
  10. }  
  11. }  
 
Output:
   Age is greater than 18


if-else statement :
  Let's see the example of if-else statement :
 
  1. //It is a program of odd and even number.  
  2. public class IfElseExample {  
  3. public static void main(String[] args) {  
  4.     //defining a variable  
  5.     int number=13;  
  6.     //Check if the number is divisible by 2 or not  
  7.     if(number%2==0){  
  8.         System.out.println("even number");  
  9.     }else{  
  10.         System.out.println("odd number");  
  11.     }  
  12. }  
  13. }  
   
Output:
         odd number

if-else-if ladder statement :
   The if-else-if ladder statement executes one condition from multiple statements.
       Let's see the example of if-else-if ladder statement :
        
      
  1. //It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.  
  2. public class IfElseIfExample {  
  3. public static void main(String[] args) {  
  4.     int marks=65;  
  5.       
  6.     if(marks<50){  
  7.         System.out.println("fail");  
  8.     }  
  9.     else if(marks>=50 && marks<60){  
  10.         System.out.println("D grade");  
  11.     }  
  12.     else if(marks>=60 && marks<70){  
  13.         System.out.println("C grade");  
  14.     }  
  15.     else if(marks>=70 && marks<80){  
  16.         System.out.println("B grade");  
  17.     }  
  18.     else if(marks>=80 && marks<90){  
  19.         System.out.println("A grade");  
  20.     }else if(marks>=90 && marks<100){  
  21.         System.out.println("A+ grade");  
  22.     }else{  
  23.         System.out.println("Invalid!");  
  24.     }  
  25. }  
  26. }  

   Output:
                 C grade

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.
      Let's see the example of if-else-if ladder statement :
            
   
  1. public class JavaNestedIfExample {    
  2. public static void main(String[] args) {    
  3.     //Creating two variables for age and weight  
  4.     int age=20;  
  5.     int weight=80;    
  6.     //applying condition on age and weight  
  7.     if(age>=18){    
  8.         if(weight>50){  
  9.             System.out.println("You are eligible to donate blood");  
  10.         }    
  11.     }    
  12.  }
  13. }  

Output:
         You are eligible to donate blood

No comments