All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Nested If Statement In Java

Last Updated : Mar 11, 2024

Nested If Statement In Java

In this article we will show you the solution of nested if statement in java, when an if statement is nested within another if statement, it is called a nested if statement.

Nested if condition statements in Java are created by nesting inner if conditions inside outer if conditions.

This decision-making statement is similar to other decision-making statements such as if, otherwise, or if...else. Similarly, in the case of nested-if statements, the code block is placed inside another if block.

In addition, the inner code block will only execute when the outer condition is met. We will now discuss the idea of how to nested if statement in java with an example.

Step By Step Guide On Nested If Statement In Java :-

import java.util.*;
public class talkerscode
    public static void main(String args[])
        scanner sc = new scanner(;
        char gender =;
        int age = sc.nextInt();
        if(age > 24){
         if(gender == 'M'){
         else if(gender == 'F'){
  1. In the first step, the java.util package is imported to provide the required utilities.
  2. In the main method of the class TalkersCode, we declare the class TalkersCode.
  3. To accept input from the user via the console, a new Scanner object named sc is created.
  4. User's first character is used as an initialization for the gender variable.
  5. An integer variable age is initialized with an integer provided by the user as the second input.
  6. Using the if statement, a check is made to determine whether the age is greater than 24. Depending on if the condition is true, the nested conditional statements will be executed, otherwise nothing will happen.
  7. When the gender is male ('M'), the corresponding println statement prints "CovidShield" to the console.
  8. The program prints "Covacine" if the gender is female ('F').
  9. In such a case, the program prints "Sputnik" when the gender is other than 'M' or 'F'.
  10. Upon termination, the program closes.

Conclusion :-

As a result, we have successfully learned how to nested if statements in java with an example.

Nested if statements are decision-making statements used when certain conditions must be met in order to make a decision.

A nested if statement in Java consists of one or more if conditions nested within one another.

In the absence of true outcomes in the outer if condition, the inner if condition is executed, otherwise the corresponding else block is executed.

It is also possible to use the nested if condition with nested if...else statements.

I hope this article on nested if statement in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪