Skip to lesson
MentorNxt home

Java Programming

Conditional Statements

Combine conditions with && and ||

One if can ask two questions at once. Join them with && when both must be true, or with || when one is enough.

&& in an if: both must be true

A hall ticket is issued only when attendance is 75% or more and the fees are paid. && (read it as "and") gives true only when both sides are true.

Example 1 · Exam eligibility with &&

HallTicket.javaJava
public class HallTicket {    public static void main(String[] args) {        int attendance = 80;        boolean feesPaid = false;        if (attendance >= 75 && feesPaid) {            System.out.println("Hall ticket issued");        } else {            System.out.println("Hall ticket on hold");        }    }}
Output
Hall ticket on hold

What this code does

  • Attendance is fine, but feesPaid is false.
  • && needs both sides to be true, so the else block runs.

One if inside another would also work, but one condition says it more simply. Logical operators (opens in a new tab) covers the full rules, including how Java skips the right side when the left side is already false.

|| in an if: one is enough

College is closed on Saturday or Sunday, and either day is enough. || gives true when at least one side is true, and false only when both sides are false.

Example 2 · Weekend check with ||

Weekend.javaJava
public class Weekend {    public static void main(String[] args) {        String day = "Sunday";        if (day.equals("Saturday") || day.equals("Sunday")) {            System.out.println("Holiday: no classes");        } else {            System.out.println("College day");        }    }}
Output
Holiday: no classes

What this code does

  • The first check is false, but the second one is true.
  • || needs only one true side, so the if block runs.

Text is compared with equals(), not ==, as in Comparison operators (opens in a new tab).

Range checks

A job form accepts ages from 18 to 35: not below 18 and not above 35. A range check joins the two comparisons with &&: age >= 18 && age <= 35.

On a number line of ages, age >= 18 covers 18 and up and age <= 35 covers 35 and below. Joined with &&, only the overlap from 18 to 35 is true. With || every age would be true.

Example 3 · Age limit as a range

AgeLimit.javaJava
import java.util.Scanner;
public class AgeLimit {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("Enter age: ");        int age = sc.nextInt();        if (age >= 18 && age <= 35) {            System.out.println("You can apply");        } else {            System.out.println("Age limit is 18 to 35");        }        sc.close();    }}
Sample output
Enter age: 38
Age limit is 18 to 35

What this code does

  • 38 >= 18 is true, but 38 <= 35 is false, so the whole && is false.
  • Both ends are included: 18 and 35 can apply.

The maths style 18 <= age <= 35 does not compile. And || is wrong here: every age is either at least 18 or at most 35, so the check would always be true.

Mixing && and ||: the leap year rule

A year is a leap year when it divides by 4 but not by 100, or when it divides by 400. So 2024 is a leap year, 1900 is not, and 2000 is.

Write each part as its own check, then join them: (year % 4 == 0 && year % 100 != 0) || year % 400 == 0. Java works out && before || (Operator precedence (opens in a new tab)), so here the brackets only make the grouping clear to the reader. Try 1900, 2000 and 2024 in the lab.

Experiment

Leap year lab

Type a year. Try 1900, 2000, 2024 and 2023.

int year = 1900;
boolean leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
System.out.println(leap);
year % 4 == 0
true
year % 100 != 0
false
year % 400 == 0
false
Leap year?
false
Sometimes brackets change the answer. Half ticket for children or seniors on weekdays is (age < 12 || age >= 60) && isWeekday. Without the brackets, Java joins age >= 60 && isWeekday first, and an 8-year-old gets a half ticket on Sunday too.

Next step

Check your understanding

8 quick questions