Java switch-case fall-through causes incorrect result without break
14:51 04 Jan 2026

I am solving the Codeforces "Bit++" problem using Java.

The program should decrease a variable when the input is "--X".

Here is my code ->>

import java.util.Scanner;

public class Main {

public static void main(String\[\] args) {

    Scanner input = new Scanner (System.in);

    int sum = 0;

    int n = input.nextInt();

    for (int i = 1; i \<= n; i++) {

       

        String x = input.next();

        switch(x){

        case "X++":

            sum = sum + 1;

          break;

        case "X--":

            sum = sum - 1;

          break;

        case "++X":

            sum = 1 + sum;

          break;

        case "--X":

            sum = sum - 1;

        }

    }

    System.out.print

ln(sum);

}

}

java switch-statement implementation fall-through