In Java, statements normally execute from top to bottom. Real-world scenarios, however, often require us to:
- Make decisions (e.g., deciding a user’s access level).
- Repeat tasks (e.g., showing a menu until a user chooses to exit).
- Handle unexpected errors without crashing.
Java’s control flow statements (e.g., if, switch, loops, branching statements, and exceptions) allow you to handle all these scenarios and more.
Comparison and Logical Operators
All control flow statements rely on boolean conditions—expressions that evaluate to either true
or false
.
Comparison Operators
Operator | Description | Example | Result |
---|---|---|---|
== |
Checks equality | 6 == 6 |
true |
!= |
Checks if values are different | 8 != 3 |
true |
> |
Checks if left side is greater | 4 > 6 |
false |
< |
Checks if left side is smaller | 2 < 9 |
true |
>= |
Greater than or equal | 5 >= 5 |
true |
<= |
Less than or equal | 12 <= 8 |
false |
Examples:
10 == 10
→true
10 != 10
→false
3 >= 5
→false
Logical Operators
Use logical operators to combine multiple comparison conditions:
-
AND (
&&
): Returnstrue
only if all conditions are true.(7 > 2) && (3 == 3)
→true && true
→true
(7 > 2) && (3 > 5)
→true && false
→false
-
OR (
||
): Returnstrue
if at least one condition is true.(1 == 1) || (2 > 10)
→true || false
→true
(5 != 5) || (2 > 10)
→false || false
→false
Decision-Making Statements
Decision-making statements let your program choose among different paths based on certain conditions.
The if
Statement
The if
statement evaluates a condition and executes specific code if that condition is true.
Example: Checking an integer score and assigning a grade band.
- First, it checks if the score is invalid (below 0 or above 100).
- Then it checks descending thresholds: 90, 80, 70, 60.
- If none match, a final else assigns the lowest grade.
Output:
Enter your score (0 – 100): 90
Grade: A
The Ternary Operator
For quick conditions that set a single value, the ternary operator is handy:
- If
score >= 60
is true,message
becomes"Pass"
. - Otherwise,
message
becomes"Fail"
.
Output:
Enter your score: 75
Pass
The switch
Statement
When you have multiple specific cases to compare, a switch
statement can be cleaner than a chain of else if
s.
- Each case checks the exact value of
languageCode
. - If no case matches,
default
runs.
Output:
Enter a language code (e.g., ‘ES’, ‘FR’): ES
Hola, Mundo!
Simplified Switch (Java 12+)
Java 12 introduced arrow case labels:
Scanner input = new Scanner(System.in);
System.out.print("Enter language code (ES, FR, DE): ");
String languageCode = input.next().toUpperCase();
switch (languageCode) {
case "ES" -> System.out.println("Hola, Mundo!");
case "FR" -> System.out.println("Bonjour, le monde!");
case "DE" -> System.out.println("Hallo, Welt!");
default -> System.out.println("Hello, World!");
}
No break
needed, but be mindful that older Java versions don’t support this syntax.
Output:
Enter language code (ES, FR, DE): FR
Bonjour, le monde!
Switch Expressions
You can also assign a value to a variable from a switch:
- If
day
is 6 or 7, the expression evaluates to"Weekend"
. - If
day
is outside 1–7, it defaults to"Unknown"
.
Looping Statements
Loops let you repeat a block of code until a certain condition is met or a certain count is reached.
The for
Loop
A for
loop is perfect when you know how many times you want to repeat something.
Explanation:
- Initialization:
int i = 1
runs once. - Condition:
i <= 4
is checked; if true, enter the loop block. - Increment:
i++
runs after each loop iteration.
Output:
Iteration #1
Iteration #2
Iteration #3
Iteration #4
Looping through an array:
Enhanced For Loop
When you only need to read each element, an enhanced for
loop is simpler:
Each loop iteration assigns an element of the array to num
.
Output:
3
6
9
12
The while
Loop
Use while
when the number of iterations isn’t known beforehand, but you keep looping as long as the condition is true:
The loop will stop when balance
is no longer greater than 0
.
Output:
Current balance: $15
Current balance: $12
Current balance: $9
Current balance: $6
Current balance: $3
The do-while
Loop
A do-while
loop runs at least once before checking its condition.
The user is prompted at least once regardless of whether the condition is true
.
Output:
Guess a number (1-5): 2
Guess a number (1-5): 3
Correct! You guessed it in 2 tries.
Branching Statements
break
break
exits the nearest loop or switch immediately.
continue
continue
skips the rest of the current loop iteration and moves on to the next iteration:
Here, 3
is never printed.
Exception Handling
Even well-written code can face unexpected errors. Exceptions allow your program to detect and handle these errors rather than abruptly crash.
Try-Catch-Finally
- try: Code that might fail (e.g., division by zero).
- catch: Handles specific exceptions.
- finally: Always executes, whether an exception was thrown or not.
Output:
Case 1: Valid Input
Enter dividend: 10
Enter divisor: 2
Result: 5
End of try–catch example.
Case 2: Division by Zero
Enter dividend: 10
Enter divisor: 0
Cannot divide by zero!
End of try–catch example.
Case 3: Invalid Input
Enter dividend: abc
Invalid input. Please enter an integer.
End of try–catch example.
Catching More Specific Errors
Java has many specialized exception classes:
ArithmeticException
: For arithmetic errors (divide by zero, overflow).InputMismatchException
: When input type doesn’t match expectations.ArrayIndexOutOfBoundsException
: When accessing invalid array indices.- And so on.
You can chain catch blocks for different errors:
Here, we even throw an exception (throw new ArrayIndexOutOfBoundsException
) if the user tries a negative index.
Output:
Case 1: Valid Input
Pick an array index (0-4): 2
Value at index 2 is 300
Case 2: Out of Bounds
Pick an array index (0-4): 5
Error: Index 5 out of bounds for length 5
Case 3: Negative Index
Pick an array index (0–4): –1
Error: Negative index!
Case 4: Invalid Input
Pick an array index (0–4): abc
Error: Enter an integer, please.
Putting It All Together
By combining these control flow tools, you can handle almost any logic:
- Comparisons and logical operators form the backbone of conditions.
- if / switch statements let you handle multiple scenarios elegantly.
- Loops (
for
,while
,do-while
, enhanced for) let you automate repetitive tasks. - Branching (
break
,continue
) refines how loops operate. - Exception handling (
try
,catch
,finally
, customthrow
) makes your code resilient against runtime errors.
Example: A small menu-driven program with error handling:
- Uses a while loop to continuously display a menu until the user chooses to exit.
- Switch statement handles different options.
- Try-catch guards against invalid integer inputs.
Output:
Case 1: Valid Addition
1. Add
2. Subtract
3. Exit
Choose an option: 1
Enter two integers to add: 10 20
Sum = 30
Case 2: Valid Subtraction
1. Add
2. Subtract
3. Exit Choose an option: 2
Enter two integers to subtract: 50 15
Difference = 35
Case 3: Invalid Option
1. Add
2. Subtract
3. Exit Choose an option: 5
Invalid option!
Case 4: Non-Integer Input
1. Add
2. Subtract
3. Exit Choose an option: abc
Error: Please enter a valid integer.
Case 5: Exit
1. Add
2. Subtract
3. Exit Choose an option: 3
Exiting…
Program terminated.
Conclusion
Control flow in Java is all about guiding the order in which statements execute, ensuring that your program behaves correctly in a variety of scenarios:
- Make decisions with
if
/else
andswitch
. - Repeat tasks with
for
,while
, anddo-while
loops. - Branch your loops with
break
andcontinue
. - Handle errors gracefully using
try-catch-finally
blocks and specific exception classes.
By mastering these constructs, you’ll be able to write flexible, error-tolerant programs that handle both normal and unexpected conditions in a clear, structured way.