Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Nested If Statement In Java
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. Java Tutorials
  3. Nested If Statement In Java

Java Tutorial

Basic JAVA Fundamentals

  • JAVA Tutorial Content
  • What Is Java
  • History Of Java
  • Java Environment
  • Features Of JAVA
  • C++ Vs Java
  • Java Basic Syntax
  • First Program Of Java
  • Java Identifiers
  • Java Modifiers
  • Java Variable Types
  • Java Basic Datatypes
  • Java Keywords

Control Statements

  • Control Statements
  • Java If Else Statement
  • Nested If Statement In Java
  • Java Switch Statement

JAVA Loops

  • Loop In Java
  • For Loop In JAVA
  • Foreach Loop In JAVA

Nested If Statement In Java

Nested if Statement in java

  • When a if condition is included in the if block of some other if condition then those are called nested ifs.
  • It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.

Syntax :

if(Condition 1) {
   // Executes when the Condition 1 is true
   if(Condition 2) {
      // Executes when the Condition 2 is true
   }
}

Java example 1 program on Nested if Statement:

if (x == 0)
{
   System.out.println("x is zero");
}
else
{
   if (x > 0)
   {
      System.out.println("x is positive");
   }
   else
   {
      System.out.println("x is negative");
   }
}

Java example 2 program on Nested if Statement:

Leap Years are any year that can be evenly divided by 4.  A year that is evenly divisible by 100 is a leap year only if it is also evenly divisible by 400.

import java.util.Scanner;  //Needed for Scanner object
public class Getleapyear
{
   public static void main(String[] args)
   {
      int year;    // holds a year
      // Create a Scanner object for keyboard input.
      Scanner scanNumber= new Scanner(System.in);
      // Get the year.
      System.out.print("Enter a year : ");
      year = scanNumber.nextInt();
      // Determine whether the year is leap year.
      if (year % 4 == 0)
      {
         if (year % 100 == 0)
         {
            if (year % 400 == 0)
            {
               System.out.println("A leap year");
            }
            else
            {
               System.out.println("Not a leap year");
            }
         }
         else
         {
            System.out.println("A leap year");
         }
      }
      else
      {
         System.out.println("Not a leap year");
      }
   }
}

Output 1

Enter a year : 2012
A leap year

Output  2

Enter a year : 1992
A leap year

Output 3

Enter a year : 2018
Not a leap year

Output 4

Enter a year : 2017
Not a leap year

Series: Java Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
else if ladder in java if else statement in java example program nested if else statement if statements java multiple conditions nested if else in javascript nested if in java definition java nested if alternative nested if java programs

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.