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
Java If Else Statement
Easy To Learning

Education Needs Complete Solution

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

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

Java If Else Statement

Decision making Statements

The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

Java if Statement

  • Based on some condition if we want to execute some set of statements then we need to use if condition in java.
  • We need to keep all the required statements inside if block .
  • If the condition is true then control executes the whole block otherwise it skips the if block of statements.

The structure of the “if” statement in Java looks like this:

Syntax :

if (condition) {
  // execute this code
}

Java example program on if control statement:

public class IfExample{
	public static void main(String[] args) {
		int age = 3;
		System.out.println("Shivani is " + age + " years old");
		if (age < 4) {
			System.out.println("Shivani is a baby");
		}
	}
}

Output :

Shivani is 3 years old
Shivani is a baby

Output Detail : In the example above we check if the age is lower than 4. As the age is set to be 3, the boolean condition age < 4 is true and as result we print “Shivani is a baby”. If we change the age to any value greater than 3 the code in the block want execute anymore and “Shivani is a baby” is not printed.

Java IF-else Statement

  • If the condition is true then control executes the whole block otherwise it skips the if block of statements
  • if condition is false  if we need to execute another set of statement we can write another if and keep all the statements in that block.
  • instead of checking multiple times we can simply use else block .

The structure of the “if-else” statement in Java looks like this:

Syntax :

if(condition){  
//code if condition is true  
}else{  
//code if condition is false  
}  

Java example program on if-else control statement:

public class IfElseExample{
	public static void main(String[] args) {
		int age = 15;
		System.out.println("Shivani is " + age + " years old");
		if (age < 4) {
			System.out.println("Shivani is a baby");
		}else{
			System.out.println("Shivani is not a baby may be adult");
        }
	}
}

Output

Shivani is 15 years old
Shivani is not a baby may be adult

Output Detail : In the example above we check if the age is lower than 4. As the age is set to be 15, the boolean condition age < 4 is true and as result we print “Shivani is a baby”. If we change the age to any value greater than 4 the code in the block want execute anymore and “Shivani is not a baby may be adult” is printed.

Java if-else-if ladder Statement

  • instead of checking multiple times we can simply use else block .
  • In some cases we may get requirement with multiple conditions in such cases we need to use if else ladder.(if -else-if-else if -else...)

The structure of the “if-else-if ladder” statement in Java looks like this:

Syntax :

if(condition1){  
//code to be executed if condition1 is true  
}else if(condition2){  
//code to be executed if condition2 is true  
}  
else if(condition3){  
//code to be executed if condition3 is true  
}  
else if(condition4){  
//code to be executed if condition4 is true  
}   
...  
else{  
//code to be executed if all the conditions are false  
}  

Java example program on if-else-if ladder control statement:

public class IfElseIfLedder{
	public static void main(String[] args) {
		int age = 20;
		System.out.println("Shivani is " + age + " years old");
		if (age < 4) {
			System.out.println("Shivani is a baby");
		} else if (age >= 4 && age < 14) {
			System.out.println("Shivani is a child");
		} else if (age >= 14 && age < 18) {
			System.out.println("Shivani is a teenager");
		} else if (age >= 18 && age < 68) {
			System.out.println("Shivani is adult");
		} else {
			System.out.println("Shivani is an old women");
		}
	}
}

Output :

Shivani is 20 years old
Shivani is adult

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:
nested if else in java if else statement java exercises java if statement multiple conditions java if statement string java if else shorthand else if ladder in java else without if java elseif java

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.