java-tutorial

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

  1. Print numbers 1..100, but:
    • print Fizz for multiples of 3
    • print Buzz for multiples of 5
    • print FizzBuzz for both
  2. Convert a number (1..7) into weekday name using switch.
  3. Sum an array of ints using both for and enhanced-for.

Table of contents

  1. Getting Started: Install, run, and your first program
  2. Java Basics: types, variables, operators, formatting
  3. Control Flow: if/switch/loops
  4. Methods: parameters, return values, overloading
  5. OOP: classes, objects, encapsulation
  6. Inheritance & Polymorphism (and when not to use them)
  7. Interfaces, abstract classes, and design basics
  8. Exceptions and error handling
  9. Strings, files, and I/O basics
  10. Collections: List/Set/Map and Big-O intuition
  11. Generics (the useful parts)
  12. Lambdas & Streams
  13. Dates and time (java.time)
  14. Testing with JUnit 5 (basics)
  15. Concurrency: threads, executors, futures
  16. JVM basics: memory, GC, performance habits
  17. Build tools: Maven essentials (recommended)
  18. Next steps: projects to build