Skip to lesson
MentorNxt home

Java Programming

Conditional Statements

if-else: choose one of two paths

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.

Syntax
if (condition) {
statements
} else {
statements
}
  1. Line 1: condition is true
  2. Line 2: condition is false
marks is 28, so marks >= 35 is false. Java skips the if block (Pass) and runs the else block (Fail). Exactly one of the two blocks runs.

Example 1 · Pass or fail

Result.javaJava
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();    }}
Sample output
Enter marks: 28
Result: Fail
Re-exam in July

What this code does

  • 28 >= 35 is false, so Java skips the if block and runs the else block.
  • The else block has two lines, and both run. Type 60 and only Result: Pass prints.

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.

7
int n = 7;
if (n % 2 == 0) {
System.out.println("even");
} else {
System.out.println("odd");
}
n % 2 (what is left over)
1
n % 2 == 0
false
The program prints
odd
n % 2 == 1 (a trap)
true
Test for even with 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

Higher.javaJava
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();    }}
Sample output
First score: 45
Second score: 72
Higher score: 72

What this code does

  • 45 > 72 is false, so the else block prints b.
  • For 60 and 60, a > b is false too, and Higher score: 60 is 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.

Java
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