Sub topic 6
Making Decisions: Control Flow Statements
So far, your programs have run from top to bottom, one line after another. Real programs need to make choices: "If the user is logged in, show the dashboard; otherwise, show the login page." In Java, we make choices with control flow statements. Control flow means deciding which lines of code run and in what order.
This chapter assumes you already know how to declare variables and use operators like >= and == from earlier chapters.
The if Statement
An if statement runs a block of code only when a condition is true. A condition is an expression that evaluates to true or false.
int temperature = 35;
if (temperature > 30) {
System.out.println("It's hot outside!");
}The code inside the curly braces { } runs only if temperature > 30 is true. Otherwise, Java skips it.
if/else: Choosing Between Two Paths
Add else to run different code when the condition is false.
int score = 75;
if (score >= 60) {
System.out.println("You passed!");
} else {
System.out.println("Try again.");
}You can chain conditions with else if to check several cases in order. Java stops at the first condition that is true.
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}Here the output is Grade: B because score >= 90 is false but score >= 80 is true, and Java skips the rest.
Comparing Values Safely
A common beginner mistake is using = (assignment) instead of == (comparison) in a condition. if (x = 5) does not compile in Java when x is an int — Java protects you here, unlike some other languages. Always use == to compare, and use .equals() to compare text:
String name = "Ada";
if (name.equals("Ada")) {
System.out.println("Hello, Ada!");
}The switch Statement
When you compare one variable against many fixed values, switch is often cleaner than a long chain of else if.
int dayNumber = 3;
String dayName;
switch (dayNumber) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
default:
dayName = "Unknown day";
break;
}
System.out.println(dayName);The break keyword stops the switch. Without it, execution "falls through" into the next case, which is usually not what you want. The default case runs when no other case matches — like the final else in an if/else chain.
The Conditional Operator ?:
For a quick either/or choice inside an expression, Java has a shorthand called the ternary operator. It has three parts: condition, value-if-true, value-if-false.
int age = 20;
String ticket = (age < 18) ? "Child ticket" : "Adult ticket";
System.out.println(ticket);This prints Adult ticket. Use it for short choices; use if/else when you need multiple lines of logic.
How the Flow Looks
Both paths eventually rejoin — only one of them runs.
Common Mistakes
- Using
=instead of==in a condition. - Forgetting
breakin aswitch, causing fall-through. - Writing
if (name == "Ada")for strings — this compares references, not the text. Use.equals(). - Missing curly braces. Java allows
if (x > 5) System.out.println("big");without braces, but adding braces{ }prevents bugs when you later add more lines.
Exercise
What does this print? int x = 10; if (x > 5) { System.out.println("A"); } else { System.out.println("B"); }
Show answer
It prints "A", because the condition x > 5 is true, so the if block runs and the else block is skipped.
Exercise
What happens in a switch statement if you forget the break keyword?
Show answer
Execution falls through into the next case's code and keeps running until it hits a break or the end of the switch, which usually causes wrong results.