Practice Control Flow in Java

Master conditional statements, loops, and program flow control in Java.

Work through 24 exercises with starter code, expected output, test cases, and hints.

How to learn Control Flow

Control flow decides which statements run and how often they run. Good control flow reads like a decision: handle invalid input, choose one valid branch, then repeat only the work that truly repeats. Deep nesting is usually a sign that the conditions or responsibilities need to be simplified.

Trace conditions with a small table before coding. Include values immediately below, on, and above each boundary. For a voting-age check that means 17, 18, and 19; for a loop over an array it means an empty array, one element, and several elements.

Recommended learning order

  1. Boolean expressions: Learn comparison operators, short-circuit && and ||, and how to name complex conditions so their intent is visible.
  2. Exclusive decisions: Use if/else-if/else when only one branch should run. Order overlapping ranges from most specific to least specific.
  3. Loops and invariants: Use for when the counter is part of the problem and while when repetition depends on a changing condition. State what remains true after every iteration.
  4. Nested iteration: Move to tables and patterns only after single loops feel natural. Count total iterations so that accidental quadratic work is visible.

Tested example: Classify a score without gaps

The guard clause handles values outside the contract. The remaining branches are ordered from the highest threshold down, so exactly one label is printed.

public class Main {
    static String grade(int score) {
        if (score < 0 || score > 100) return "invalid";
        if (score >= 90) return "A";
        if (score >= 75) return "B";
        if (score >= 60) return "C";
        return "D";
    }

    public static void main(String[] args) {
        System.out.println(grade(75));
        System.out.println(grade(101));
    }
}

Expected output

B
invalid

Change and run this example in the Java compiler

Common mistakes

  • Writing separate if statements for mutually exclusive ranges, allowing more than one branch to run.
  • Using <= where < is intended at a boundary, or forgetting the exact boundary altogether.
  • Creating a loop whose control variable never changes on every path, which can make the loop infinite.
  • Using break to repair unclear loop conditions instead of first expressing the stopping rule directly.

How to practice

Begin with decisions, then counting loops, then data-dependent loops, and finish with nested loops. For every solution, list the boundary cases and manually trace the loop state for at least three iterations before pressing Run.

Exercises to do first

  • Voting age — Practice one inclusive boundary and make the under-age case explicit.
  • Grade calculator — Order overlapping score ranges so exactly one grade is chosen.
  • Prime check — Stop trial division when the divisor passes the square root of the number.

Control Flow exercises

  1. Check Voting Age Java exercise — Determine if someone can vote
  2. Grade Calculator Java exercise — Convert a score to a letter grade
  3. Leap Year Check Java exercise — Determine if a year is a leap year
  4. Print Numbers 1-5 Java exercise — Use a for loop to print numbers
  5. Print Even Numbers Java exercise — Print even numbers in a range
  6. Multiplication Table Java exercise — Print multiplication table for a number
  7. Prime Number Check Java exercise — Check if a number is prime
  8. Nth Fibonacci Number Java exercise — Find the nth Fibonacci number
  9. Print Primes in Range Java exercise — Print all prime numbers in a range
  10. Sum of Series Java exercise — Calculate 1 + 1/2 + 1/3 + ... + 1/n
  11. Number Pattern Java exercise — Print a number pattern
  12. Star Pyramid Java exercise — Print a centered star pyramid
  13. Digit Frequency Java exercise — Count occurrences of a digit in a number
  14. Decimal to Binary Java exercise — Convert decimal number to binary
  15. Sum Until Condition Java exercise — Sum numbers until a condition is met
  16. Diamond Pattern Java exercise — Print a diamond pattern
  17. Collatz Conjecture Java exercise — Generate Collatz sequence for a number
  18. Digital Root Java exercise — Find the digital root of a number
  19. Abundant Number Check Java exercise — Check if sum of proper divisors exceeds the number
  20. Kaprekar Number Check Java exercise — Check if a number is a Kaprekar number
  21. Neon Number Check Java exercise — Check if a number is a Neon number
  22. Advanced Leap Year Java exercise — Determine leap year with century rule
  23. Number Classification Java exercise — Classify a number by multiple properties
  24. Number Pattern Spiral Java exercise — Print numbers in a specific pattern

Browse the complete Java practice path · Open the online Java compiler