java if else

Java If else statement-: Like any other language In java If ..else statement is a powerful statement. It is a condition statement and decision making statement. If is a Java keyword and it is  followed by two condition between which we put the statement to be evaluated. If Java determines that the statement we write between the condition is true  the code in the following brackets will execute. If Java determines that the statement is false the process is terminated.

Types of if else statement in java

  • If statement
  • if else statement
  • Nested if statement
  • If else if ladder statement

 

If statement -: This is simple condition statement like any other language c and c++ . for  understanding if statement we take a example

 

public class IfExample { 

public static void main(String[] args) { 

              int i=22; 
              if(i>18){ 

             System.out.print("i is greater than 18"); 

             } 
        } 
} 



Output of this programme 
i is grater than 18

 

If ..else statement in java -:  In the if .. else statement in java first its checks the If condition . If it is true then execute the process of body .when the condition is false it  check the else condition. The syntax

 

If(test expression ) …………//condition is true

{

true block statement ;

}
else......//statement
{
  false statement bolck;
}

If the ext expression is true then the first condition is block and if the first condition is not the true the else condition will be block .

For example write a programme for check ood and even number in java

public class netnic { 

public static void main(String[] args) { 

  int number=22; 
  if(number%2==0){ 

              System.out.println("The number is even"); 

        }else{ 

              System.out.println("The number is odd "); 
    } 
  } 
} 

Output of this programme

The number is even

 

Nested If  else statement in java-: In Java  series  of decisions are involved we may have to use more than one if ……..else statement in programme it is called nested if else statement. Syntax of nested if else statement ..

if ( test condition 1 )   // condition is true
  {
  if (Test condition 2)
   {
    True block statement 1;
   }
 else         // statement false
  {
    True block statement 1;
  }
 else  
   {
    false statement 2 block;
   }
statement x;
}
  

Write a example of nested if ..else statement

//Java Program to demonstrate the use of Nested If Statement. 

public class JavaNestedIfExample2 { 

public static void main(String[] args) { 


int age=35; 
int weight=48; 
          
//condition apply
 
if(age>=18){ 
if(weight>50){ 
              System.out.println("You are eligible playing cricket"); 
} else{
              System.out.println("You are eligible for football "); 
}
} else{
System.out.println("Age must be greater than 18");
}
} }

output of this programme

You are eligible for football 

 

Java if else ladder statement -: There is another method  of putting IF condition  together when multiple decisions are involved a multiple decision is a chain of If  in which the statement associate with each else  is an if. it takes the following general form

If (Test expression)

statement 1;

else if (test expression 2)

statement;

else if (test expression 3)

statement ;

else if (test expression n)

statement x;


 

For example write a programme to a class student with percent.

public class IfElseIfladderexample {  
public static void main(String[] args) {  
    int marks=82;  
      
    if(marks<50){  
        System.out.println("fail");  
    }  
    else if(marks>=50 && marks<60){  
        System.out.println("D grade");  
    }  
    else if(marks>=60 && marks<75){  
        System.out.println("C grade");  
    }  
    else if(marks>=75 && marks<80){  
        System.out.println("B grade");  
    }  
    else if(marks>=80 && marks<90){  
        System.out.println("A grade");  
    }else if(marks>=90 && marks<100){  
        System.out.println("A+ grade");  
    }else{  
        System.out.println("Invalid!");  
    }  
}  
} 

Output of this programme

A grade

 

Leave a Reply

Your email address will not be published. Required fields are marked *