codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

13 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java If...Else
☕Java Programming

Java If...Else

Updated 2026-05-12
15 min read

Java If...Else

In programming, making decisions based on conditions is a fundamental task. Java provides several constructs to handle these decisions, including the if, else if, and else statements, as well as the ternary operator for concise conditional expressions. Understanding how to use these constructs effectively will enhance your ability to write robust and dynamic Java programs.

Introduction

Conditional statements allow your program to execute different blocks of code based on whether certain conditions are true or false. This is crucial for creating interactive applications that respond to user input, handle various scenarios, and make logical decisions.

In this tutorial, you'll learn how to use the if, else if, and else statements to control the flow of your Java programs. Additionally, we'll explore the ternary operator as a shorthand for simple conditional expressions.

The Basics of If Statements

The if statement is used to execute a block of code only if a specified condition evaluates to true. Here's the basic syntax:

Java
1if (condition) {
2 // Code to be executed if the condition is true
3};

Example 1: Basic If Statement

Let's start with a simple example where we check if a number is positive.

IfExample.java
1public class IfExample {
2 public static void main(String[] args) {
3 int number = 5;
4
5 if (number > 0) {
6 System.out.println("The number is positive.");
7 }
8 }
9}
Output
The number is positive.

Explanation

  • We declare an integer variable number and assign it the value 5.
  • The if statement checks if number is greater than 0. Since this condition is true, the code inside the curly braces is executed.
  • If the condition were false (e.g., number = -5), the code block would not be executed.

Adding Else Statements

The else statement allows you to execute a different block of code when the if condition is false. This provides an alternative path for your program.

Java
1if (condition) {
2 // Code to be executed if the condition is true
3} else {
4 // Code to be executed if the condition is false
5}

Example 2: If-Else Statement

Here's an example where we check if a number is positive or negative.

IfElseExample.java
1public class IfElseExample {
2 public static void main(String[] args) {
3 int number = -3;
4
5 if (number > 0) {
6 System.out.println("The number is positive.");
7 } else {
8 System.out.println("The number is negative or zero.");
9 }
10 }
11}
Output
The number is negative or zero.

Explanation

  • We check if number is greater than 0. Since this condition is false, the code inside the else block is executed.
  • The else block provides an alternative action when the if condition fails.

Using Else If Statements

The else if statement allows you to test multiple conditions in sequence. It's useful when there are more than two possible outcomes.

Java
1if (condition1) {
2 // Code to be executed if condition1 is true
3} else if (condition2) {
4 // Code to be executed if condition1 is false and condition2 is true
5} else {
6 // Code to be executed if all previous conditions are false
7}

Example 3: If-Else If-Else Statement

Let's check if a number is positive, negative, or zero.

IfElseIfExample.java
1public class IfElseIfExample {
2 public static void main(String[] args) {
3 int number = 0;
4
5 if (number > 0) {
6 System.out.println("The number is positive.");
7 } else if (number < 0) {
8 System.out.println("The number is negative.");
9 } else {
10 System.out.println("The number is zero.");
11 }
12 }
13}
Output
The number is zero.

Explanation

  • The program checks each condition in order.
  • If number > 0 is true, it prints "The number is positive."
  • If the first condition is false, it checks if number < 0. Since this condition is also false, it moves to the else block.
  • The else block executes when all previous conditions are false.

Ternary Operator

The ternary operator provides a concise way to write simple conditional expressions. It's a shorthand for an if-else statement.

Java
1result = (condition) ? expression1 : expression2;

Example 4: Using the Ternary Operator

Let's use the ternary operator to determine if a number is even or odd.

TernaryExample.java
1public class TernaryExample {
2 public static void main(String[] args) {
3 int number = 4;
4
5 String result = (number % 2 == 0) ? "Even" : "Odd";
6
7 System.out.println("The number is " + result);
8 }
9}
Output
The number is Even.

Explanation

  • The ternary operator checks if number % 2 == 0. If true, it assigns the string "Even" to result.
  • If false, it assigns "Odd" to result.
  • This concise syntax is useful for simple conditional assignments.

Practical Example: A Simple Grade Calculator

Let's create a complete program that calculates and displays a student's grade based on their score.

GradeCalculator.java
1public class GradeCalculator {
2 public static void main(String[] args) {
3 int score = 85;
4
5 if (score >= 90) {
6 System.out.println("Grade: A");
7 } else if (score >= 80) {
8 System.out.println("Grade: B");
9 } else if (score >= 70) {
10 System.out.println("Grade: C");
11 } else if (score >= 60) {
12 System.out.println("Grade: D");
13 } else {
14 System.out.println("Grade: F");
15 }
16 }
17}
Output
Grade: B

Explanation

  • The program checks the student's score and assigns a grade based on predefined ranges.
  • It uses multiple else if statements to handle different scenarios.

Summary

ConceptDescription
If StatementExecutes code if a condition is true.
Else StatementExecutes code if the preceding conditions are false.
Else If StatementTests additional conditions in sequence after an if or another else if.
Ternary OperatorProvides a concise way to write simple conditional expressions.

What's Next?

In the next tutorial, we'll explore the switch statement, which is used for executing one block of code among many alternatives. This will allow you to handle multiple conditions in a more organized manner.

To continue learning, compile and run the examples provided in this tutorial using the following commands:

Terminal
$ javac IfExample.java
$ java IfExample
$ javac IfElseExample.java
$ java IfElseExample
$ javac IfElseIfExample.java
$ java IfElseIfExample
$ javac TernaryExample.java
$ java TernaryExample
$ javac GradeCalculator.java
$ java GradeCalculator

Happy coding!


PreviousJava BooleansNext Java Switch

Recommended Gear

Java BooleansJava Switch