Skip to content
Laviyx Learn

Try

Java Control Flow Practice

Practise if/else, switch expressions, for and while loops, nested loops, break and continue in Java.

Beginner7 problemsUpdated

Review first: if, else if and else, switch and switch Expressions, Loops: for, while, do-while, for and Enhanced for, while and do-while, break and continue

  1. 1.Marks to grades

    Beginner

    For each mark in {95, 82, 67, 45, 74}, print the mark and its grade: A for 90+, B for 75–89, C for 60–74 and F below 60.

    Show hint

    Use an if / else if chain from the highest grade down.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int[] marks = {95, 82, 67, 45, 74};
            for (int m : marks) {
                char grade;
                if (m >= 90) {
                    grade = 'A';
                } else if (m >= 75) {
                    grade = 'B';
                } else if (m >= 60) {
                    grade = 'C';
                } else {
                    grade = 'F';
                }
                System.out.println(m + " " + grade);
            }
        }
    }
    Output
    95 A
    82 B
    67 C
    45 F
    74 C
  2. 2.FizzBuzz

    Beginner

    Print 1 to 15 on one line, separated by spaces, with Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for multiples of both.

    Show hint

    Check n % 15 == 0 first. A StringBuilder builds the line.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            StringBuilder line = new StringBuilder();
            for (int n = 1; n <= 15; n++) {
                if (n > 1) line.append(' ');
                if (n % 15 == 0) line.append("FizzBuzz");
                else if (n % 3 == 0) line.append("Fizz");
                else if (n % 5 == 0) line.append("Buzz");
                else line.append(n);
            }
            System.out.println(line);
        }
    }
    Output
    1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
  3. 3.Weekday or weekend

    Beginner

    Days are numbered 1 (Monday) to 7 (Sunday). For 1, 6, 7 and 9, print weekday, weekend or invalid using a switch expression.

    Show hint

    Arrow cases can list several labels: case 1, 2, 3, 4, 5 -> "weekday";.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            for (int day : new int[] {1, 6, 7, 9}) {
                String kind = switch (day) {
                    case 1, 2, 3, 4, 5 -> "weekday";
                    case 6, 7 -> "weekend";
                    default -> "invalid";
                };
                System.out.println(day + ": " + kind);
            }
        }
    }
    Output
    1: weekday
    6: weekend
    7: weekend
    9: invalid
  4. 4.Digit sum and reverse

    Beginner

    For n = 50721, use a while loop to print the sum of its digits and the number reversed.

    Show hint

    Take the last digit with n % 10, remove it with n /= 10, and build the reverse with reversed * 10 + digit.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int n = 50721;
            int sum = 0;
            int reversed = 0;
            while (n > 0) {
                int digit = n % 10;
                sum += digit;
                reversed = reversed * 10 + digit;
                n /= 10;
            }
            System.out.println(sum + " " + reversed);
        }
    }
    Output
    15 12705
  5. 5.First common multiple

    Beginner

    Find the first number greater than 100 that is divisible by both 7 and 11, using a loop with break.

    Show hint

    Count upwards from 101 and break as soon as both remainders are 0.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int n = 101;
            while (true) {
                if (n % 7 == 0 && n % 11 == 0) break;
                n++;
            }
            System.out.println(n);
        }
    }
    Output
    154
  6. 6.Star pyramid

    Beginner

    Print a centred pyramid of stars with 4 rows: row r has 4 - r spaces and 2r - 1 stars.

    Show hint

    " ".repeat(n) and "*".repeat(n) (Java 11) build each part of a row.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int height = 4;
            for (int r = 1; r <= height; r++) {
                System.out.println(" ".repeat(height - r) + "*".repeat(2 * r - 1));
            }
        }
    }
    Output
       *
      ***
     *****
    *******
  7. 7.Skip the negatives

    Beginner

    Add up only the positive numbers in {5, -2, 8, -7, 3}, using continue to skip the others.

    Show hint

    if (x < 0) continue; at the top of the loop body.

    Show solution
    Java
    public class Main {
        public static void main(String[] args) {
            int total = 0;
            for (int x : new int[] {5, -2, 8, -7, 3}) {
                if (x < 0) continue;
                total += x;
            }
            System.out.println(total);
        }
    }
    Output
    16