DEV Community

Kesavarthini
Kesavarthini

Posted on

Decision Making Statements in Java – A Beginner-Friendly Guide

When learning Java, writing programs is not just about executing code line by line.
Sometimes, a program needs to make decisions based on certain conditions.
This is where decision making statements come into play.

In this blog, I’ll explain the decision making statements in Java with simple explanations and examples suitable for beginners.

🔹 What Are Decision Making Statements?

Decision making statements allow a program to choose different paths of execution based on conditions.
These conditions usually return either true or false.

Java provides several decision making statements to control the flow of execution.

🔹 Types of Decision Making Statements in Java

1️⃣ if Statement

The if statement executes a block of code only when the condition is true.

Syntax:

if (condition) {
    // code executes if condition is true
}
Enter fullscreen mode Exit fullscreen mode

Example:

int age = 20;

if (age >= 18) {
    System.out.println("Eligible to vote");
}
Enter fullscreen mode Exit fullscreen mode

2️⃣ else Statement

The else statement executes code only if the condition is false.

Syntax:

if (condition) {
    // true block
} else {
    // false block
}
Enter fullscreen mode Exit fullscreen mode

Example:

int number = 5;

if (number % 2 == 0) {
    System.out.println("Even number");
} else {
    System.out.println("Odd number");
}
Enter fullscreen mode Exit fullscreen mode

3️⃣ else–if Statement

This is used when multiple conditions need to be checked.

Syntax:

if (condition1) {
    // code
} else if (condition2) {
    // code
} else {
    // default code
}
Enter fullscreen mode Exit fullscreen mode

Example:

int marks = 75;

if (marks >= 90) {
    System.out.println("Grade A");
} else if (marks >= 75) {
    System.out.println("Grade B");
} else {
    System.out.println("Grade C");
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)