3) Control Flow
Goal
Make decisions and repeat work using if, switch, and loops.
if / else
int score = 82;
if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else {
System.out.println("C or below");
}
switch
Modern switch (recommended):
String day = "MON";
String type = switch (day) {
case "SAT", "SUN" -> "weekend";
default -> "weekday";
};
while / do-while
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}
for loops
for (int n = 0; n < 5; n++) {
System.out.println(n);
}
Enhanced for-loop:
int[] values = {10, 20, 30};
for (int v : values) {
System.out.println(v);
}
Exercises
- Print numbers 1..100, but:
- print
Fizz for multiples of 3
- print
Buzz for multiples of 5
- print
FizzBuzz for both
- Convert a number (1..7) into weekday name using
switch.
- Sum an array of ints using both
for and enhanced-for.
Table of contents
- Getting Started: Install, run, and your first program
- Java Basics: types, variables, operators, formatting
- Control Flow: if/switch/loops
- Methods: parameters, return values, overloading
- OOP: classes, objects, encapsulation
- Inheritance & Polymorphism (and when not to use them)
- Interfaces, abstract classes, and design basics
- Exceptions and error handling
- Strings, files, and I/O basics
- Collections: List/Set/Map and Big-O intuition
- Generics (the useful parts)
- Lambdas & Streams
- Dates and time (java.time)
- Testing with JUnit 5 (basics)
- Concurrency: threads, executors, futures
- JVM basics: memory, GC, performance habits
- Build tools: Maven essentials (recommended)
- Next steps: projects to build