Java – Conditional Statements, IF, Else-IF, Else


What is a conditional statements ?

Conditional statements is also termed as conditional expressions which is symbolised by variable A and B, is an if-then-else statement in which A is a hypothesis and B is a conclusion.

what is the purpose of conditional statement in java ?

Conditional statement is used to decide which piece of code should be executed when the condition mets TRUE and which piece of code should be executed when the condition mets FALSE.

Types of Conditional Statements

There are two types of conditional statements

1. IF – Else,If- Else

2. Switch –  case ( will be explained in next tutorial )

IF – Else Statement 

IF – statement executes the piece of code when the condition is true.

Else IF – statement executes the piece of code when the second condition is true.

Else – statement executes the piece of code when the condition is false.

IF Condition :

Syntax :

if (condition){
statements
}

1. Comparing two variables.


The condition should be of boolean value true/false.

Only, if the condition is true, it will execute the piece of code and prints the output, there is no else part so if the condition is false it prints nothing.

Example 1 :

public class Eg1{
public static void main(String args[])
{
int A = 10;
int B = 10;
if (A==B){
System.out.println("A and B are equal");
}
}
}

Output :

A and be are equal

2. Comparing two Characters.

The condition should be of boolean value true/false.

Only, if the condition is true, it will execute the piece of code and prints the output, there is no else part so if the condition is false it prints nothing.

Example 1 :

public class Eg1{
        public static void main(String args[])
        {
                char A = 'H';
                char B = 'H';
                if (A==B){
                System.out.println("char A and char B are equal");
                }
        }
}

Output :

char A and char B are equal

3. Comparing two String variables.

The condition should be of boolean value true/false.

Only, if the condition is true, it will execute the piece of code and prints the output, there is no else part so if the condition is false it prints nothing.

Example 1 :

public class Eg1{
public static void main(String args[])
{
String A = "Happy";
String B = "Happy";
  if (A.equals(B)){
    System.out.println("String A and String B are equal");
  }
}
}

Output :

String A and String B are equal

IF-Else condition :

Syntax :

if (condition){

Statements;
}
else{
Statements;
}

1. Comparing two variables.

The condition should be of boolean value true/false.

Only, if condition is true it executes the piece of code and if the condition is false it will execute the piece of code in the else part.

Example 1 :

public class Eg1{
public static void main(String args[])
{
int A = 10;
int B = 20;
  if (A==B){
    System.out.println("int A and int B are equal");
  }
  else{
    System.out.println("int A and int B are not equal");
  }
}
}

Output :

int A and int B are not equal

2. Comparing two character.

The condition should be of boolean value true/false.

Only, if condition is true it executes the piece of code and if the condition is false it will execute the piece of code in the else part.

Example 1 :

public class Eg1{
        public static void main(String args[])
        {
                char A = 'H';
                char B = 'Z';
                if (A==B){
                System.out.println("char A and char B are equal");
                }
                else{
                System.out.println("char A and char B are not equal");
                }
        }
}

Output :

char A and char B are not equal

3. Comparing two Strings

The condition should be of boolean value true/false.

Only, if condition is true it executes the piece of code and if the condition is false it will execute the piece of code in the else part.

Example 1 :

public class Eg1{
        public static void main(String args[])
        {
                String A = "Happy";
                String B = "BirthDay";
                if (A.equals(B)){
                System.out.println("String A and String B are equal");
                }
                else{
                System.out.println("String A and String B are not equal");
                }
        }
}

Output :

String A and String B are not equal.

IF – Else,IF – Else Condition :

Syntax :

if(condition1){
statements...
}
else if(condition1){
statements...
}
else if(condition3){
statements...
}
else if(condition4){
statements...
}
else{
statements...
}

1. Comparing three variables.

The condition should be of boolean value true/false.

It checks for the condition1 and if its false it checks for the condition2 and prints the output.

Example 1 :

public class Eg1{
public static void main(String args[])
{
int A = 10;
int B = 20;
int C = 10;
  if (A==B){
    System.out.println("int A and int B are equal");
  }
  else if(A==C)
  {
    System.out.println("int A and int C are equal");
  }
  else{
    System.out.println("int A and int B are not equal");
  }
}
}

Output :

int A and int C are equal

2. Comparing three characters.

The condition should be of boolean value true/false.

It checks for the condition1 and if its false it checks for the condition2 and prints the output.

Example 1 :

public class Eg1{
public static void main(String args[])
{
char A = 'x';
char B = 'y';
char C = 'x';
  if (A==B){
    System.out.println("char A and char B are equal");
  }
  else if(A==C)
  {
    System.out.println("char A and char C are equal");
  }
  else{
    System.out.println("char A and char B are not equal");
  }
}
}

Output :

char A and char C are equal

3. Comparing three String variables.

The condition should be of boolean value true/false.

It checks for the condition1 and if its false it checks for the condition2 and prints the output.

Example 1 :


public class Eg1{
public static void main(String args[])
{
String A = "Happy";
String B = "BirthDay";
String C = "Happy";
  if (A.equals(B)){
    System.out.println("String A and String B are equal");
  }
  else if(A.equals(C))
  {
    System.out.println("String A and String C are equal");
  }
  else{
    System.out.println("String A and String B are not equal");
  }
}
}

Output :

String A and String C are equal 

Nested IF-Else,IF-Else

Nested if-else, if-else condition, is if-else condition inside if-else condition.


The condition should be of boolean value true/false.

Syntax :

if(condition1){
Statements...
}
else if(condition2)
{
Statements...
if(conditionA){
Statements...
}
else if(conditionB)
{
Statements...
}
else{
Statements...
}
}
else if(condition3){
Statements...
if(conditionX)
{
Statements...
}
}
else{
Statments...
}

condition1, condition2 and condition3 are the boolean conditions for IF statements.

conditionA, conditionB and conditionX are the boolean conditions for Nested-IF Statements.

Example 1 :

public class Eg2{
public static void main(String args[])
{
int a = 10;
int b = 20;
int c = 20;
String A = "Happy";
String B = "Birthday";
char x = 'A';
char y = 'B';
char z = 'A';
  if(a==b){
    System.out.println("int variable a = "+a+ " is equal to int variable b = "+b);
  }
  else if(b==c){
    System.out.println("int variable b = "+b+ " is equal to int variable c = "+c);
      if(A.equals(B)){
         System.out.println("String A = "+A+ " is equal to String B = "+B);
      }
      else{
         System.out.println("String A = "+A+ " is not equal to String B = "+B);
     if(x==y)
     {
         System.out.println("char x = "+x+" is equal to char y = "+y);
     }
     else if(y==z)
     {
         System.out.println("char y = "+y+" is equal to char z = "+z);
     }
     else if(x==z)
     {
         System.out.println("char x = "+x+" is equal to char z = "+z);
     }
   }
}
else if(a==c){
  System.out.println("int variable a = "+a+ " is equal to int variable c = "+c);
}
else if(a!=b)
{
  System.out.println("int variable a = "+a+ " is not equal to int variable b = "+b);
}
else{
  System.out.println("No condition satisfied");
}

}
}

Output :

int variable b = 20 is equal to int variable c = 20

String A = Happy is not equal to String B = Birthday

char x = A is equal to char z = A