Loading...

Making Decisions with If–Else in Java

A Simple Thought

Every day, you make choices, what to wear, what to eat, or whether to go out depending on the weather. Computers, too, make decisions. They can decide what message to show, which action to take, or what to calculate, all based on conditions.

In this lesson, you’ll teach Java how to make choices using if–else statements.
By the end, you’ll build a small Exam Result Checker that decides if a student passes or fails based on marks.

The Project: Exam Result Checker

Imagine a simple school system that checks if a student has passed the exam.

  • If the marks are 50 or above, it prints “You Passed!”
  • If the marks are below 50, it prints “You Failed!”

Your output will look like this:

text
5 lines
|
25/ 500 tokens
1
2
3
4
5
========================
   EXAM RESULT
   Marks: 67
   Status: You Passed!
========================

The computer will decide what to print, not you.

The Concept: Conditional Statements

So far, your programs printed or calculated things without asking questions.
But sometimes you need to tell the computer:

“If this is true, do this. Otherwise, do something else.”

That’s exactly what if–else statements do.

Here’s the basic pattern:

java
5 lines
|
18/ 500 tokens
1
2
3
4
5
if (condition) {
    // do something
} else {
    // do something else
}
Code Tools

For example:

java
7 lines
|
32/ 500 tokens
1
2
3
4
5
6
7
 int marks = 67;

if (marks >= 50) {
    System.out.println("You Passed!");
} else {
    System.out.println("You Failed!");
}
Code Tools

Here, Java checks the condition marks >= 50. If it’s true, it runs the first block. If it’s false, it runs the second.

Why This Matters

Every decision-based program, from games to grading apps, uses conditions.
They’re what allow computers to respond differently depending on what’s happening.

Think about it:

  • A traffic signal turns red or green based on timers.
  • A bank system allows withdrawals only if the balance is enough.
  • A login system accepts or rejects passwords depending on a match.

Without if–else, everything would behave the same way, and that would be boring.

Let’s Build the Result Checker

Here’s the full working code:

java
17 lines
|
121/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ExamResult {
    public static void main(String[] args) {
        int marks = 67;

        System.out.println("========================");
        System.out.println("   EXAM RESULT");
        System.out.println("   Marks: " + marks);

        if (marks >= 50) {
            System.out.println("   Status: You Passed!");
        } else {
            System.out.println("   Status: You Failed!");
        }

        System.out.println("========================");
    }
}
Code Tools

If you change the value of marks to 40, the output will change automatically to “You Failed!” You’ve just built your first decision-making program.

How It Works

  1. The program stores marks in a variable.
  2. Java checks whether marks is greater than or equal to 50.
  3. If true, it prints “You Passed!”
  4. Otherwise, it prints “You Failed!”

The computer doesn’t guess, it evaluates logic step by step, just like a real examiner.

Learn Together with AI

You can ask your AI Copilot to write and modify condition-based programs for you.

Try this prompt:

“Write a Java program that checks if a student passes or fails based on marks using an if–else statement.”

Once you get the code, ask it to:

  • Add a new condition for “Excellent” if marks are 90 or more.
  • Or make it show “Needs Improvement” if marks are between 40 and 49.

Watch how AI instantly expands your program’s decision power, one rule at a time.

Practice Challenge

Now it’s your turn!
Create a program that checks if a person is eligible to vote.
The rule is simple:

  • If the person’s age is 18 or older, print “You are eligible to vote.”
  • Otherwise, print “You are not eligible yet.”

The output should look like this:

text
5 lines
|
28/ 500 tokens
1
2
3
4
5
========================
   VOTER CHECK
   Age: 21
   Result: You are eligible to vote.
========================

Example Solution

java
17 lines
|
126/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class VoterCheck {
    public static void main(String[] args) {
        int age = 21;

        System.out.println("========================");
        System.out.println("   VOTER CHECK");
        System.out.println("   Age: " + age);

        if (age >= 18) {
            System.out.println("   Result: You are eligible to vote.");
        } else {
            System.out.println("   Result: You are not eligible yet.");
        }

        System.out.println("========================");
    }
}
Code Tools

Try changing the age to 15 or 30 and watch how the output changes. Your program is now officially thinking before it speaks.

Coming Up Next

You’ve taught Java how to decide. Next, you’ll show it how to repeat tasks automatically using loops, the key to building programs that can handle lists, patterns, and repetitive actions all by themselves.