Control Flow and Loops
Branch with if and switch, repeat with for, while, and the for-each loop.
Making decisions
Java's decision-making will feel familiar — the if/else if/else chain with conditions in parentheses and blocks in braces:
int marks = 72;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 60) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
Java checks each branch top to bottom and runs the first whose condition is true, then skips the rest — so order matters (we check 90 before 75 before 60). Use braces even for single-line blocks; it's a professional habit that prevents bugs.
switch for many fixed values
When testing one variable against several specific values, switch is cleaner than a long chain:
int day = 3;
switch (day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Other day");
}
The break after each case is essential — without it, execution falls through into the next case. default handles anything unmatched. (Modern Java has a newer arrow-style switch too, but this classic form is what you'll see most and should learn first.)
The for loop
Java's for loop packs start, condition, and step into one line — the workhorse for counting:
for (int i = 1; i <= 5; i++) {
System.out.print(i + " "); // 1 2 3 4 5
}
// Sum 1 to 100
int total = 0;
for (int i = 1; i <= 100; i++) {
total += i;
}
System.out.println(total); // 5050
Read the three parts: int i = 1 runs once at the start; i <= 5 is checked before each pass; i++ runs after each pass. This compact form is dense but flexible.
The for-each loop
When you simply want to visit every element of an array or collection, Java's for-each loop is cleaner and safer — no index to manage, no off-by-one mistakes:
int[] scores = {88, 72, 95, 60};
// for-each: "for each score in scores"
for (int score : scores) {
System.out.print(score + " "); // 88 72 95 60
}
String[] names = {"Mehul", "Riya", "Arjun"};
for (String name : names) {
System.out.println("Hello, " + name);
}
Read for (int score : scores) as "for each score in scores" — exactly like Python's for. Use it whenever you don't need the index; use the classic for when you do. (We met arrays here — collections of values; the next chapter covers them properly.)
while, do-while, and loop control
A while loop repeats while its condition holds — for when you don't know the count in advance. do-while checks at the bottom, so its body always runs at least once:
int count = 5;
while (count > 0) {
System.out.print(count + " ");
count--; // must move toward the exit!
}
// 5 4 3 2 1
Inside any loop, break exits immediately and continue skips to the next pass:
for (int n = 1; n <= 10; n++) {
if (n == 7) break; // stop at 7
if (n % 2 == 0) continue; // skip even numbers
System.out.print(n + " ");// 1 3 5
}
As always with while, make sure something inside eventually makes the condition false, or you'll loop forever. With control flow in hand, we can group code into methods and store collections in arrays — the subject of the next chapter.
Finished "Control Flow and Loops"?
Mark this chapter complete so you can pick up exactly where you left off. Your progress saves locally — sign in to sync across devices.
Was this chapter clear?
