if-else: choose one of two paths
16 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)An if-else statement chooses between two paths. When the condition is true, the if block runs; otherwise the else block runs.
if-else: one of two blocks
A result card must show either Pass or Fail, never both and never neither. else means "otherwise": it has no condition of its own and runs exactly when the if condition is false.
- Line 1: condition is true
- Line 2: condition is false
Example 1 · Pass or fail
import java.util.Scanner;
public class Result { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter marks: "); int marks = sc.nextInt(); if (marks >= 35) { System.out.println("Result: Pass"); } else { System.out.println("Result: Fail"); System.out.println("Re-exam in July"); } sc.close(); }}Enter marks: 28 Result: Fail Re-exam in July
What this code does
28 >= 35isfalse, so Java skips theifblock and runs theelseblock.- The
elseblock has two lines, and both run. Type 60 and onlyResult: Passprints.
Two separate if statements would need the check twice, as marks >= 35 and marks < 35. Change one, forget the other, and the card shows both results or none. With else, exactly one block always runs.
Even or odd: a remainder check
A quiz app gives even-numbered questions to one team and odd ones to the other. How can a program tell even from odd?
A number is even when it divides by 2 with nothing left over. The remainder operator % from Arithmetic operators (opens in a new tab) gives what is left over, so the test is n % 2 == 0. Try the lab with a positive and a negative number.
Experiment
Even or odd lab
Press − and + to change n. Try 7, then -7, and compare the last two rows.
- n % 2 (what is left over)
- 1
- n % 2 == 0
- false
- The program prints
- odd
- n % 2 == 1 (a trap)
- true
n % 2 == 0. The test n % 2 == 1 misses negative odd numbers: -7 % 2 is -1, not 1.The larger of two numbers
Two friends compare their test scores: which one is higher? One question decides it: if a > b is true, print a; otherwise print b.
Example 2 · Higher of two scores
import java.util.Scanner;
public class Higher { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("First score: "); int a = sc.nextInt(); System.out.print("Second score: "); int b = sc.nextInt(); if (a > b) { System.out.println("Higher score: " + a); } else { System.out.println("Higher score: " + b); } sc.close(); }}First score: 45 Second score: 72 Higher score: 72
What this code does
45 > 72isfalse, so theelseblock printsb.- For 60 and 60,
a > bisfalsetoo, andHigher score: 60is still correct.
To report "equal" as a third answer, you need a third path: The else-if ladder (opens in a new tab) adds it.
if-else or ?:
Sometimes both paths only pick a value, like the word "Pass" or "Fail". The ternary operator (opens in a new tab) ?: does that in one line.
int marks = 72;String result = marks >= 35 ? "Pass" : "Fail"; // ?: picks a value
if (marks >= 35) { // if-else runs a block System.out.println("Pass"); System.out.println("Certificate ready");} else { System.out.println("Fail");}Use ?: when you only need one of two values to store or print. Use if-else when a path does more: prints several lines, changes variables or reads more input.
Next step
Check your understanding
8 quick questions