Java If else


Decision making structures helps to the programme evaluate one or more conditions in the program. If the condition is true it executes true statement otherwise it executes the false statement.

  • If Statement
  • If-else Statement
  • Nested If statement
  • Switch Statement
  • Nested Switch Statement
Java If else Flow

If Statement:-

The if statement has a Boolean expression which contains one or more statements. The if condition is true it executes true statement.

if(boolean_expression) {
  /* statements will execute if the boolean expression is true */
} 

Example:-

public class If
{  
    public static void main(String []args) {  
        int a=10;
        if(a!=0)  
        {   
            System.out.println("Print the value of a "+a); 
        }
    }  
} 

If-else Statement:-

The if-else statement functions execute code statements if the test expression is true and some other code statements if the test expression is false.

Java If else Statement

Syntax:-

 if(boolean_expression) {
	/* statements will execute if the boolean expression is true */
}
else{
	/* statements will execute if the boolean expression is false */
}

Example

 public class Ifelse
{  
    public static void main(String []args) {  
        int a=10,b=15;
        if(a > b) {   
    		System.out.println("Print the value of a "+a); 
        }
        else{
            System.out.println("Print the value of b "+b); 
        }
	}  
}

Nested If Statement:-

Nested If statement is used if or if-else inside another if or if-else.

Syntax:-

 if( boolean expression ) {
	/* statements will execute if the boolean expression is true */
   if(boolean expression 1) {
      /* statements will execute if the boolean expressionc 1 is true */
   }
}

Example:-

 public class Nestedif
{  
    public static void main(String []args) {  
        int a=12,b=7,c=5;
        if(a > b) {   
            if(a > c){
                System.out.println("Print the value of a "+a); 
            }
            else{
                System.out.println("Print the value of c "+c); 
            }
        }  
        else{
            if(b > c){
                System.out.println("Print the value of b "+b); 
            }
             else{
                System.out.println("Print the value of c "+c);  
             }
        }
	}  
}



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.