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.
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
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); // CJava 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
| Operator | Name | True when |
|---|---|---|
&& | AND | both sides are true |
|| | OR | at least one side is true |
! | NOT | the value is false (it flips a boolean) |
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:
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); // WednesdayThe 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
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: ₹100Is 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
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.
