Combine conditions with && and ||
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)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 &&
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"); } }}Hall ticket on hold
What this code does
- Attendance is fine, but
feesPaidisfalse. &&needs both sides to betrue, so theelseblock 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 ||
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"); } }}Holiday: no classes
What this code does
- The first check is
false, but the second one istrue. ||needs only onetrueside, so theifblock 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.
Example 3 · Age limit as a range
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(); }}Enter age: 38 Age limit is 18 to 35
What this code does
38 >= 18istrue, but38 <= 35isfalse, so the whole&&isfalse.- 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.
- year % 4 == 0
- true
- year % 100 != 0
- false
- year % 400 == 0
- false
- Leap year?
- false
(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