Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Conditional Statements in Java?
3.
Types of Java Conditional Statements
4.
Java If Statement
4.1.
Syntax 
4.2.
Working of Java If Statement
4.3.
Example 
4.4.
Java
5.
Java If-Else Statement
5.1.
Syntax 
5.2.
Working of Java If-Else Statement
5.3.
Example 
5.4.
Java
5.5.
Using Ternary Operator
5.6.
Java
6.
Java If-Else-If Ladder Statement
6.1.
Syntax 
6.2.
Working of Java If-Else-If Ladder Statement
6.3.
Example
6.4.
Java
7.
Java Nested If Statement
7.1.
Syntax 
7.2.
Working of Java Nested If Statement
7.3.
Example 
7.4.
Java
8.
Java Switch Statement
8.1.
Syntax 
8.2.
Working of Java Switch Statement
8.3.
Example
8.4.
Java
9.
Java If-Else vs Java Switch Statements
10.
Frequently Asked Questions
10.1.
What are the conditional statements in Java?
10.2.
What are the 4 conditional statements in Java?
10.3.
What are conditional statements in OOP?
11.
Conclusion
Last Updated: Mar 27, 2024
Easy

Conditional Statements in Java (If-Else Statement)

Author Ayush Mishra
0 upvote

Introduction

Conditional statements, like the if-else statement in Java, control program flow based on conditions. The if-else syntax consists of an initial condition followed by code blocks for true and false outcomes. It enables executing different code paths depending on whether a condition evaluates to true or false.

conditional statements in java

In this blog, we will discuss Conditional Statements in Java (If-Else Statement) in detail. Let's start going!

What are Conditional Statements in Java?

Conditional statements in Java are utilized to make decisions in a program based on some specific conditions. The main constraints are ‘if,’ ‘else,’ and ‘else if.' These statements allow the program to execute different lines of code depending on the scenario, whether a particular condition is true or false. These are vital for controlling the flow of a program that enables the developers to create the logic that is able to respond dynamically to the data or the input, which further helps in making the program more powerful and flexible.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Types of Java Conditional Statements

There are five types of Java conditional statements. All of these are widely used according to their syntaxes and use cases. We will discuss them further in detail so let us note them down below:

1. Java If Statement

2. Java If-Else Statement

3. Java If-Else-If Ladder Statement

4. Java Nested If Statement

5. Java Switch Statement

Java If Statement

Java if statement

Suppose a condition is true if a statement is used to run the program. It is also known as a one-way selection statement. If a condition is used, an argument is passed, and if it is satisfied, the corresponding code is executed; otherwise, nothing happens.

Syntax 

The syntax of the If statement is:-

if (expression) {
    // You can enter the code here
}

Working of Java If Statement

If conditional statements in java works on the following given statement:-

1. Statements in the block are executed if the expression evaluates to nonzero.

2. Control is transferred to the statement that follows the expression if it evaluates to zero (false).

Example 

The below given example helps in understanding the if statement in Java.

  • Java

Java

import java.util.*;
class sample {
   public static void main(String[] args) { 
       int a = -3;   
       if (a < 0)  {   
           System.out.println(a + " is a Negative Number.");   
       } 
   }
}

Output:

Output of If statement

Java If-Else Statement

Java If-Else Statement

An if-else statement is a control structure that determines which statements to choose based on a set of conditions.

Syntax 

The syntax of the If-Else conditional statements in java is:-

if (condition) {
    // Statements that will be carried out if the condition is satisfied
} else {
    // Statements that will be carried out if the condition is not met
}

Working of Java If-Else Statement

The statements in the if block is carried out if the condition is met. Otherwise, the statements in the else block are carried out. Always follow an if clause with an else clause. If not, the compiler will produce a "misplaced else" error.

Example 

The example below helps us understand the if-else conditional statements in Java.

  • Java

Java

import java.util.*;
class sample {  
   public static void main(String[] args) {     
       int a = 10;      
       if (a % 2 == 0)  {          
           System.out.println(a + " is an even number");      
       }      
       else  {          
           System.out.println(a + " is an odd number");        
       }  
   }
}

Output:

Output of If-Else statement

Using Ternary Operator

The Java ternary operator offers a concise syntax for determining if a condition is true or false. It returns a value based on the outcome of the Boolean test.

One can replace their Java if-else statement using a ternary operator to create compact code. Expert coders enjoy the clarity and simplicity the Java ternary operator adds to their code.

The syntax of the Java Ternary operator is as follows:

(condition) ? (return if true) : (return if false);

 

Let's understand the concept with the help of a code example. We will see the above code using the ternary operator instead of the Java If-else statement.

  • Java

Java

class example {
public static void main( String args[] ) {
int a = 10;
String teropt = (a % 2 == 0) ? a + " is an even number" : a + " is an odd number";
System.out.println(teropt);
}
}

 

Output

10 is an even number

Java If-Else-If Ladder Statement

An if-else-if ladder in Java can execute one code block while multiple other blocks are executed.

Java If-Else-If Ladder Statement

Syntax 

The syntax of the If-Else-If conditional statements in java is:-

if (condition1) { 
    // Executable code if condition1 is true,
} else if (condition2) { 
    // Executable code if condition2 is true  
} 


... 
else { 
    // Executable code if all the conditions are false  
}

Working of Java If-Else-If Ladder Statement

The conditional expressions are evaluated in the If-Else-If statement from top to bottom. The statement associated with a true condition is executed as soon as it is found, and the rest of the ladder is skipped. The final else statement is executed if all the conditions are false.

Example

The below given example helps in understanding the if-else-if ladder conditional statements in Java.

  • Java

Java

import java.util.*;
class sample {  
   public static void main(String[] args) {      
       int i = 3;      
       if (i == 1) 
            System.out.println("January");      
       else if (i == 2)          
            System.out.println("February");      
       else if (i == 3) 
            System.out.println("March");      
       else 
            System.out.println("April");        
   }
}

Output:

Output of if-else-if ladder statement

Java Nested If Statement

If conditions inside another if conditions are called as Nested If conditional statements in java.

Java Nested If Statement

Syntax 

The syntax of the nested if conditional statements in java are:-

if (condition1) {   
    // Statement 1 will execute
      
    if (condition2)   {  
      // Statement 2 will execute
    }
}

Working of Java Nested If Statement

The way nested if statements operate assumes that they must first become true and sufficient for the other states with the second condition for them to be used, even though other statements may choose to proceed with a false condition if the first condition is met.

Example 

The example below helps us understand the Nested If conditional statements in Java.

  • Java

Java


       System.out.println("i is smaller than 15 too");
     else
       System.out.println("i is greater than 20");
   }
 }
}

Output:

output of nested if statement

 

Java Switch Statement

Unlike the if-else statement, the switch statement has more than one way to be executed. Additionally, it compares the expression's value to its cases by focusing its evaluation on a few primitive or class types.

Syntax 

The syntax of the Switch statement is:-

switch (Expression) {
    case value 1:
        // Statement 1;
    case value 2:
        // Statement 2;
    case value 3:
        // Statement 3;
        
         ...
    case value n:
        // Statement n;
    Default:
        // default statement;
}

Working of Java Switch Statement

Java's switch case allows multiple conditions to be checked simultaneously, similar to an if-else ladder. A constant or a literal expression that can be evaluated is given to Switch.

Each test case's value is compared to the expression's value until a match is made. The associated code is executed if the specified default keyword does not find a match. Otherwise, the code designated for the test case that matches it is run.

Example

  • Java

Java

import java.util.*;
class Sample {
 public static void main(String[] args) {
   int mon = 2;
   switch (mon) {
   case 1:
     System.out.println("January");
     break;
    
   case 2:
     System.out.println("February");
     break;
    
   case 3:
     System.out.println("March");
     break;
    
   case 4:
     System.out.println("April");
     break;
    
   case 5:
     System.out.println("May");
     break;
    
   case 6:
     System.out.println("June");
     break;
    
   default:
     System.out.println("Not present in first half month of year");
   }
 }
}

Output:

output of switch statement

Practice and compile it on online java compiler.

Java If-Else vs Java Switch Statements

If-Else Statement Switch Statement
Best for checking multiple conditions that can't be represented by a single expression Best for checking a single expression against multiple values
Evaluates a boolean expression to determine which branch to execute Evaluates an expression to determine which case to execute
Can handle complex conditions with multiple boolean expressions Can handle a limited set of values that can be matched
Can use all relational operators Can only use the == operator to compare values

Frequently Asked Questions

What are the conditional statements in Java?

Conditional statements in Java are used to make decisions based on certain conditions. The most common conditional statements in Java are the If-Else statement, the Switch statement, and the Ternary Operator. These statements allow the program to execute different blocks of code based on specific conditions.

What are the 4 conditional statements in Java?

The four conditional statements in Java are: if, if-else, else-if ladder, and switch. The 'if-else' statement executes different code based on whether the condition is true or false. The 'switch' statement evaluates an expression and executes the code corresponding to the matching case.

What are conditional statements in OOP?

In OOPs, conditional statements are used to tell the computer what action to take in response to certain conditions. The execution flow may alter if a condition is added to a block of statements depending on the result of the condition's evaluation. 

Conclusion

We have discussed the Conditional Statements in Java (If-Else Statement). We further discussed the If, If-Else, and switch conditional statements in Java.

We hope this blog has helped you enhance your knowledge of Conditional Statements in Java. Do not stop learning! We recommend you read some of our Java articles:-

 

We wish you Good Luck! 

Happy Learning!

Live masterclass