If statement in Java

If statement in Java is a simple decision-making statement. It is used to decide whether the statement or block of statements will be executed or not. Block of statements will be executed if the given condition is true otherwise the block of the statement will be skipped.

if(condition) 
{
   // If condition is  true then block of statements will be executed 
}

Here, the condition will be always in boolean form. It will be either true or false. if statement accepts only boolean value in condition. If a user doesn’t provide curly braces (‘{‘ ‘}’ ) then by default only one statement will be considered inside the if block.

if(condition) 
//This statement will be consider in if block it will be execute if condition is true    
statement 1; 
//This statement will not be consider in if block it doesn’t depend on condition.  
statement 2; 

Flowchart of if statement

If statement in Java
class ExampleIfStatement 
{ 
   public static void main(String args[]) 
   { 
      int i = 8; 
      if (i > 5)  // Statement will be executed if value of “i”  is greater than 5
      {
         System.out.println("Number is greater than 5"); 
      }	 
      System.out.println("Statement after the if block"); 
      } 
}

Output:
Number is greater than 5
Statement after the if block

What type of condition can have an if statement in Java?

The condition of if statement can be an expression or any boolean variable.
First, we will understand it with an expression that will return a boolean value. We can use any expression as a condition that gives results in boolean.

Example of expression:
5 > 3, It returns true because 5 is greater than 3.
5 > (3+3), It returns false because 5 is less than 6.
int a = 10;
int b = 15;
int c = 12;
a > b, It returns false because a(10) is less than b(15).
(a+b) > c, It returns true because a+b(25) is greater than c(12).

Let’s create an example, In which if the condition can take the expression of different types.

class ExampleIfStatement 
{ 
   public static void main(String args[]) 
   { 
      if (5 > 3)  // Statement will be executed because 5 is greater than 3
      {
         System.out.println("The Number 5 greater than 3"); 
      }	
      
      int a = 10;
      int b = 12;
      int c = 15;
      
      if (a < b)  // Statement will be executed because b is greater than a
      {
         System.out.println("The value of b is greater than a"); 
      }	
      if ((a+b) > c)  // Statement will be executed because a+b is greater than c
      {
         System.out.println("The value of a+b is greater than c"); 
      }	
    } 
}

Output: The Number 5 greater than 3
The value of b is greater than a
The value of a+b is greater than c

We can directly use a boolean value as a condition in the if statement. Let’s create an example with a boolean value.

Example of expression:
true,
false

class ExampleIfStatement 
{ 
   public static void main(String args[]) 
   { 
      if(true)  // Statement will be executed because we are using true
      {
         System.out.println("The statement executes because the value is true."); 
      }	
      if(!false)  // Statement will be executed because we are inverting the false
      {
         System.out.println("The statement executes because the value is inverting of false."); 
      }	
    } 
}

Output: The statement executes because the value is true.
The statement executes because the value is inverting of false.

1. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{  
     public static void main(String[] args)
     {  
    	 if(true)
    		 System.out.println("First If has executed");
    	 
    	 if(false)
    		 System.out.println("Second If has executed");
    	 
    	 System.out.println("Outside the if statement");
     }  
}

 

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{
   public static void main(String[] args)
   {
       if(null)
       System.out.println("First If has executed");
       if(!null)
       System.out.println("Second If has executed");
   }
}

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{
   public static void main(String[] args)
   {
      if(0)
      System.out.println("First If has executed");
      if(1)
      System.out.println("Second If has executed");
   }
}

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{
   public static void main(String[] args)
   {
      if(false)
         System.out.println("First If has executed");
      System.out.println("Second If has executed");
   }
}

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class ExampleIfStatement
{
   public static void main(String[] args)
   {
      int a = 5, b = 10;
      if(a > b)
         System.out.println("a is greater than b");
      if(b > a)
         System.out.println("b is greater than a");
   }
}

Click on anyone to know the answer.

Leave a Comment