Java & Spring Boot Enterprise Mastery · Chapter 2 · ~15 min · Beginner
Control Flow, Loops & The String Equality Trap
Teach your Java programs how to think and repeat tasks: conditional branching with if / else, fixed collections with arrays, repeating code with loops, and avoiding the infamous String == String reference bug.
- Make decisions with if, else if, else, &&, and ||
- Store ordered collections with fixed-size arrays
- Iterate through items using for and enhanced for-each loops
- Master why strings MUST be compared with .equals() instead of ==
1Making Decisions with if, else if, and else
Real software doesn't just run top-to-bottom blindly; it reacts to user input and conditions. In Java, we make decisions using if statements:
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: Needs Improvement");
}Logical Operators
• && (AND): Both conditions must be true:
if (age >= 18 && hasLicense) { ... }• || (OR): At least one condition must be true:
if (isWeekend || isHoliday) { ... }Comparison Operators
==Equals (primitives only!)!=Not equals>/>=Greater than / Greater or equal</<=Less than / Less or equal
What is printed if 'int age = 16;' and code evaluates: if (age >= 18) { ... } else { System.out.println('Minor'); }?
2Storing Multiple Values with Arrays
What if you need to track 100 test scores or 50 customer names? Declaring 100 separate variables would be unbearable. Instead, Java provides Arrays:
// Create an array with 4 items:
int[] scores = {88, 92, 79, 95};
// Access items by zero-based index:
System.out.println(scores[0]); // Prints 88 (first item)
System.out.println(scores[3]); // Prints 95 (last item)
System.out.println(scores.length); // Prints 4 (total count)For an array String[] names = {'Alice', 'Bob', 'Charlie'}, what is names[1]?
3Repeating Actions with Loops
Loops allow you to run a block of code over and over again without rewriting it.
1. The Classic For Loop
Best when you need the counter index i:
for (int i = 0; i < scores.length; i++) {
System.out.println("Score #" + (i + 1) + ": " + scores[i]);
}2. The Enhanced For-Each Loop
Clean and readable when you just want to visit every element in order:
for (int score : scores) {
System.out.println("Processing score: " + score);
}How many times will this loop run: for (int i = 0; i < 5; i++)?
4The Infamous String Equality Trap (== vs .equals())
This is the single most common bug that trips up every new Java programmer and interview candidate.
When you use == with primitive numbers (int, double), it checks if the numerical values are equal. But with Objects like Strings, == checks if they point to the exact same memory address!
Compares memory references, so even identical string text can evaluate to false:
String input = scanner.nextLine(); // user types "secret"
// BUG: compares memory pointer addresses, not string text!
if (input == "secret") {
System.out.println("Access Granted");
}Always call .equals() to compare character contents:
// ALWAYS use .equals() to compare strings in Java!
if (input.equals("secret")) {
System.out.println("Access Granted");
}How should you compare whether two String variables (str1 and str2) contain the exact same characters?
5Hands-On: Step Through a Java Loop Live
Click "Next Step" to watch an accumulator loop calculate an average score and evaluate a final letter grade:
Step-by-Step Java Loop & Conditional Stepper
Array in Memory: int[] scores = {88, 92, 79, 95};
Current Executing Line:
int sum = 0;Initialize sum accumulator to 0 before loop begins.
6Chapter Knowledge Challenge
Test your Java control flow, array indices, and loop mechanics before moving to Chapter 3:
For an array 'int[] ages = new int[5];', what is the index of the final element?
Why does 'new String('admin') == 'admin'' return false in Java?
7Recap
- Use
if,else if, andelseto branch program execution. - Combine boolean expressions with
&&(AND) and||(OR). - Arrays are zero-indexed fixed-size collections. Access elements with
arr[0]and find length witharr.length. - Use enhanced
for (Type item : collection)loops for concise iteration. - Never use
==to compare Strings! Always usestr1.equals(str2).
Congratulations on completing Chapter 2! Mark as complete to update your syllabus: