Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 10

Operators & Control Flow

Operators perform computations; control flow determines which code runs and when. Together they are the engine of every algorithm you will ever write. Modern Java (14+) also has enhanced switch expressions worth knowing from day one.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Operators Reference

  • Arithmetic: + - * / % (modulo = remainder). 10 % 3 = 1
  • Assignment: = += -= *= /= %= (shorthand: x += 5 means x = x + 5)
  • Comparison: == != > < >= <= (return boolean)
  • Logical: && (AND) || (OR) ! (NOT)
  • Ternary: condition ? valueIfTrue : valueIfFalse
  • Bitwise: & | ^ ~ << >> (advanced - used in flags, performance-critical code)

Control Flow - if, switch, loops

Control Flow - if, switch, loops
public class ControlFlow {
    public static void main(String[] args) {
        // if / else if / else
        int score = 85;
        if (score >= 90) {
            System.out.println("Grade: A");
        } else if (score >= 80) {
            System.out.println("Grade: B");  // ← this runs
        } else {
            System.out.println("Grade: C");
        }

        // Ternary - concise single-line condition
        String result = (score >= 50) ? "Pass" : "Fail";
        System.out.println(result);  // Pass

        // Traditional switch (Java 7)
        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;  // ← this runs
            default: System.out.println("Weekend");
        }

        // Switch Expression (Java 14+) - cleaner, no fall-through issues
        String dayName = switch (day) {
            case 1 -> "Monday";
            case 2 -> "Tuesday";
            case 3 -> "Wednesday";
            default -> "Weekend";
        };
        System.out.println(dayName);  // Wednesday

        // for loop - known count
        for (int i = 1; i <= 5; i++) {
            System.out.print(i + " ");  // 1 2 3 4 5
        }
        System.out.println();

        // while loop - unknown count
        int n = 1;
        while (n <= 100) { n *= 2; }
        System.out.println("First power of 2 over 100: " + n);  // 128

        // for-each - iterate collections/arrays
        String[] languages = {"Java", "Python", "Go"};
        for (String lang : languages) {
            System.out.println(lang);
        }
    }
}

Quick Quiz

Tip

Tip

Practice Operators Control Flow in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Operators & Control Flow

Practice Task

Note

Practice Task - (1) Write a working example of Operators Control Flow from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Operators Control Flow is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Key Takeaways

  • Operators perform computations; control flow determines which code runs and when.
  • Arithmetic: + - * / % (modulo = remainder). 10 % 3 = 1
  • Assignment: = += -= *= /= %= (shorthand: x += 5 means x = x + 5)
  • Comparison: == != > < >= <= (return boolean)

Topics in This Module

0/10 done