Skip to main content

Lesson 3 of 5 15 min read

Conditional statements and logical operators

Make decisions with if, else if, else and switch, and combine conditions with &&, || and !.

In this lesson you will

  • Control which code runs with if, else if and else
  • Combine conditions with the logical operators &&, || and !
  • Choose between if-chains and a switch expression

Making decisions

Programs often need to do different things in different situations. A conditional statement runs a block of code only when a condition is true.

Java
int marks = 72;

if (marks >= 40) {
    System.out.println("You passed.");
} else {
    System.out.println("Try again next time.");
}

The condition inside the parentheses must be a boolean expression. Exactly one of the two blocks runs.

Several possibilities with else if

Java
int marks = 72;
String grade;

if (marks >= 90) {
    grade = "A";
} else if (marks >= 75) {
    grade = "B";
} else if (marks >= 60) {
    grade = "C";
} else {
    grade = "Needs improvement";
}

System.out.println(grade); // C

Java checks the conditions from top to bottom and runs the first block whose condition is true. Order matters: if you checked marks >= 60 first, a score of 95 would wrongly get a C.

Logical operators

OperatorNameTrue when
&&ANDboth sides are true
||ORat least one side is true
!NOTthe value is false (it flips a boolean)
Java
int age = 22;
boolean hasId = true;

if (age >= 18 && hasId) {
    System.out.println("Entry allowed");
}

boolean isWeekend = false;
boolean isHoliday = true;
if (isWeekend || isHoliday) {
    System.out.println("Office closed");
}

if (!hasId) {
    System.out.println("Please bring an ID card");
}

Java uses short-circuit evaluation: in a && b, if a is false, b is never checked; in a || b, if a is true, b is skipped. This lets you write safe checks such as name != null && name.length() > 0.

The switch expression

When you compare one value against many fixed options, a switch is easier to read. Modern Java (14+) supports a concise arrow form that returns a value:

Java
int day = 3;
String name = switch (day) {
    case 1 -> "Monday";
    case 2 -> "Tuesday";
    case 3 -> "Wednesday";
    case 4 -> "Thursday";
    case 5 -> "Friday";
    case 6, 7 -> "Weekend";
    default -> "Invalid day";
};
System.out.println(name); // Wednesday

The default branch handles every value not listed. Use switch for exact matches and if chains for ranges such as marks >= 75.

Walkthrough: a ticket price calculator

Java
int age = 67;
boolean isStudent = false;
int price;

if (age < 5) {
    price = 0;
} else if (age >= 60 || isStudent) {
    price = 100;
} else {
    price = 200;
}
System.out.println("Ticket price: ₹" + price); // Ticket price: ₹100
The decision tree Java follows (the first true condition wins)

Is age < 5? (contains Yes: price = 0, No: Is age >= 60 or isStudent? (contains Yes: price = 100, No: price = 200))

Trace it: age < 5 is false, so Java checks the next condition. age >= 60 is true, so the OR expression is true without checking isStudent, and the price is 100.

Hands-on exercise

Write a program that stores a temperature in Celsius and prints "Cold" below 15, "Pleasant" from 15 to 30 inclusive, and "Hot" above 30. Then add a boolean isRaining and print "Take an umbrella" only when it is raining and the temperature is below 25.

Knowledge check

3 questions • 70% to pass • Practice only, no certificate

  1. 1.What is printed when marks is 80?
    CODE
    if (marks >= 60) {
        System.out.println("C");
    } else if (marks >= 75) {
        System.out.println("B");
    } else {
        System.out.println("Fail");
    }
  2. 2.When is the expression (a || b) false?
  3. 3.How should you check whether the String input contains the text "yes"?
0 of 3 answered

Key concepts

  • if, else if and else run the first block whose condition is true, checked from top to bottom.
  • && needs both conditions true, || needs at least one, and ! flips a boolean.
  • Java short-circuits && and ||, skipping the right side when the result is already known.
  • Use equals to compare Strings and switch for exact matches against fixed values.