Control FlowCore· 30 min read

Making Decisions (if / else)

if / else lets your program choose what to do based on whether a condition is true.

What you will learn

  • Write an if statement
  • Add else and else if branches
  • Combine conditions with && and ||

Choosing a path

Programs often need to decide. An if statement runs a block of code only when a condition is true. Add else to run something different when it is false.

A basic if / else
int score = 72;

if (score >= 50) {
    System.out.println("You passed!");
} else {
    System.out.println("Try again.");
}

Note: Output: You passed! The condition score >= 50 is true (72 is at least 50), so the first block runs and the else block is skipped.

Many choices with else if

For more than two outcomes, chain else if. Java checks each condition top to bottom and runs the first one that is true.

Grading with else if
int score = 72;

if (score >= 90) {
    System.out.println("Grade: A");
} else if (score >= 75) {
    System.out.println("Grade: B");
} else if (score >= 50) {
    System.out.println("Grade: C");
} else {
    System.out.println("Grade: Fail");
}

Note: Output: Grade: C 72 is not >= 90, and not >= 75, but it IS >= 50, so we get Grade: C. Once a branch matches, the rest are skipped.

Combining conditions

  • && (AND) — true only when both sides are true.
  • || (OR) — true when at least one side is true.
  • ! (NOT) — flips true to false and back.
AND combines two conditions
int age = 20;
boolean hasTicket = true;

if (age >= 18 && hasTicket) {
    System.out.println("Welcome in!");
}

Note: Output: Welcome in! Both sides are true (age is 18 or more AND they have a ticket), so && is true and the message prints. If either were false, nothing would print.

Now compare that with || (OR), which only needs one side to be true — handy when there are several ways to qualify:

OR is true when at least one side is true
boolean isWeekend = false;
boolean isHoliday = true;

if (isWeekend || isHoliday) {
    System.out.println("No work today!");
}

Note: Output: No work today! It is not the weekend, but it IS a holiday. Because || only needs one side to be true, the whole condition is true and the message prints. With && here, it would have stayed silent (both sides would need to be true).

LeftRight&& (AND)|| (OR)
truetruetruetrue
truefalsefalsetrue
falsefalsefalsefalse

Tip: The condition inside if (...) must be a boolean — something that is true or false. That is exactly what the comparison operators from the last unit produce.

Q. With && (AND), when is the whole condition true?

Answer: && needs BOTH sides to be true. If you only need one side true, use || (OR) instead.

✍️ Practice

  1. Read a number with Scanner and print whether it is positive, negative or zero using if / else if / else.
  2. Check with && whether a number is between 10 and 20 (inclusive).

🏠 Homework

  1. Write a program that reads a temperature and prints Cold (below 15), Nice (15 to 28) or Hot (above 28).
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →