shaydel
shaydel

Reputation: 589

why is the wrong "case" being executed after "default" in a switch statement

How is it possible that the output is 1002,why is the last case being executed despite having a mismatch?

public class Main {
    public static void main(String[] args) {
        int i=0,j=0;
        switch (i) {
            case 2 : j++;
            default: j+=2;
            case 15 : j+=1000;
        }
        System.out.println("j="+j);
    }
}

Upvotes: 0

Views: 3421

Answers (5)

PSR
PSR

Reputation: 40338

FALLTHROUGH:

Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

Your code should be:

  case 2 : j++; break;
  case 4:  j+=10; break;
  default: j+=2; break;
  case 15: j+=1000;
}

FROM DOCS

public class Example{

public static void main(String[] args) {
    java.util.ArrayList<String> futureMonths =
        new java.util.ArrayList<String>();

    int month = 8;

    switch (month) {
        case 1:  futureMonths.add("January");
        case 2:  futureMonths.add("February");
        case 3:  futureMonths.add("March");
        case 4:  futureMonths.add("April");
        case 5:  futureMonths.add("May");
        case 6:  futureMonths.add("June");
        case 7:  futureMonths.add("July");
        case 8:  futureMonths.add("August");
        case 9:  futureMonths.add("September");
        case 10: futureMonths.add("October");
        case 11: futureMonths.add("November");
        case 12: futureMonths.add("December");
        default: break;
    }

    if (futureMonths.isEmpty()) {
        System.out.println("Invalid month number");
    } else {
        for (String monthName : futureMonths) {
           System.out.println(monthName);
        }
    }
}

}

This is the output from the code:

August
September
October
November
December

Upvotes: 9

Harish Kumar
Harish Kumar

Reputation: 528

In this case case 2 and case 4 are not execute but default and case 15 are so the answer is 1002. Please put break statement for desired result.

Hope this helps.

Upvotes: -1

Tony
Tony

Reputation: 1276

you don't have the 'break' keyword specified in each of your cases.

Should be like this:

switch (i){
    case 2 : 
    j++;
    break;
    case 4:
    j+=10;
    break;
    case 15 : 
    j+=1000;
    break;
    default: 
    j+=2;
    break;
}

Upvotes: -1

Peter Jaloveczki
Peter Jaloveczki

Reputation: 2089

You have to break at the end of the case blocks. Otherwise all subsequent cases will be also executed.

public class Main {
public static void main(String[] args) {
    System.out.println("Hello World!");
    int i=0,j=0;
    switch (i){
        case 2 : j++; break;
        case 4: j+=10; break;
        case 15 : j+=1000; break;
        default: j+=2;
    }
    System.out.println("j="+j);
}
}

Upvotes: 1

sanbhat
sanbhat

Reputation: 17622

Because you are missing break;

and if I understand your confusion, The order of default doesn't matter. In below case,

    int i=15,j=0;
    switch (i){
        case 2 : 
            j++;
            break;
        case 4: 
            j+=10;
            break;
        default: 
            j+=2;
            break;
        case 15 : 
            j+=1000;
            break;
    }

j will be having value 1000 even if default was before case 15

Upvotes: 0

Related Questions