stephenwade
stephenwade

Reputation: 1199

Java: Can I fall through only one case in a switch statement

In Java, can I fall through only one of the cases in a switch statement? I understand that if I break, I will fall through to the end of the switch statement.

Here's what I mean. Given the following code, on case 2, I want to execute case 2 and case 1. On case 3, I want to execute case 3 and case 1, but not case 2.

switch(option) {
    case 3:  // code
             // skip the next case, not break
    case 2:  // code
    case 1:  // code
}

Upvotes: 18

Views: 20581

Answers (7)

Thomas Bui
Thomas Bui

Reputation: 308

You can custom the condition if you want to split cases

const { type, data } = valueNotifications

    let convertType = ''
    if (data?.type === 'LIVESTREAM' && type === 'NORMAL') { 
      convertType = 'LIVESTREAM1' 
    } else convertType = type 

    switch (convertType) 

My use case has split the type from value notifications, but I have a specific LiveStream case which only shows in data.type is 'LIVESTREAM'

Upvotes: 0

millimoose
millimoose

Reputation: 39950

My suggestion is to not use fallthrough for anything except cases like the following:

switch (option) {
    case 3:
        doSomething();
        break;
    case 2:
    case 1:
        doSomeOtherThing();
        break;
    case 0:
        // do nothing
        break;
}

That is, giving several cases the exact same block of code to handle them (by "stacking" the case labels), making it more or less obvious what the flow is here. I doubt most programmers intuitively check for case fall through (because the indentation makes a case look like as a proper block) or can efficiently read code that relies on it - I know I don't.

Upvotes: 11

m0skit0
m0skit0

Reputation: 25873

Put the code into methods and call as appropriate. Following your example:

void case1() {
    // Whatever case 1 does
}

void case2() {
    // Whatever case 2 does
}

void case3() {
    // Whatever case 3 does
}

switch(option) {
    case 3:
        case3();
        case1();
        break;
    case 2:
        case2();
        case1();
        break;
    case 1: 
        case1();   // You didn't specify what to do for case 1, so I assume you want case1()
        break;
    default:
        // Always a good idea to have a default, just in case demons are summoned
}

Of course case3(), case2()... are very poor method names, you should rename to something more meaningful about what the method actually does.

Upvotes: 12

ZhongYu
ZhongYu

Reputation: 19682

switch(option) 
{
    case 3:
        ...
        break;
    case 2: 
        ...
        break;
}

... // code for case 1

Upvotes: 1

gerrytan
gerrytan

Reputation: 41123

In switch statement if you don't break the subsequent case is executed. To give you simple example

    int value = 2;
    switch(value) {
    case 1: 
        System.out.println("one");
        break;
    case 2: 
        System.out.println("two");
    case 3: 
        System.out.println("three");
        break;
    }

Will output

two
three

Because break wansn't executed on case 2

Upvotes: -2

ceruleus
ceruleus

Reputation: 704

Something like this maybe.

switch(option) {
    case 3:  // code
             // skip the next case, not break
        // BLOCK-3
    case 2:  // code
        if(option == 3) break;
        // BLOCK-2
    case 1:  // code
        // BLOCK-1
}

Upvotes: -1

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

No, what you are after is not possible with a switch statement. You will fall through each case until you hit a break. Perhaps you want case 1 to be outside of your switch statement, so that it is executed regardless.

Upvotes: 13

Related Questions