Mahesh Nadar
Mahesh Nadar

Reputation: 487

Understanding switch behaviour

Consider the following code

int x = 1
switch(x) {
    case 1:
        System.out.println("1");
    case 2:
        System.out.println("2");
    default:
        System.out.println("no value found");
 }

it prints

1
2
no value found

as expected because there is no break in each case statement
my doubt is that how execution goes to each case statement if there is no break in first one
because x is not equal 2 then also its execute case 2 block
but i understood default one because without break program continue its execution and execute the default statement

Upvotes: 2

Views: 272

Answers (9)

Joey
Joey

Reputation: 354824

This is a remnant of the C programming language. In C switch is little more than a little syntactic sugar over a goto depending on the expression value and thus execution simply jumps to the appropriate case and continues from there. The labels in between are exactly that: labels. They are jump targets but don't affect control flow otherwise and from the compiler's point of view there is nothing in a label that merits a jump elsewhere (except there is INTERCAL's COMEFROM somewhere). So you have to put an explicit break after every case where you don't want to fall through to other options.

Java more or less inherited those semantics while eschewing some of the crazier C idioms.

C# on the other hand goes a bit further by disallowing fall-through entirely on non-empty case labels.

In my opinion though, this was a bit of a design error. Fall-through by default may be much easier to implement in the compiler (because you don't have to add a goto to the end of the switch and it aligns very nicely with how things actually work underneath), but in my experience there are vastly more programming errors by accidentally using fall-through than by accidentally not using it.

The most severe design error in my eyes is including the switch statement with all its weirdness in modern languages.

Upvotes: 2

Bharat Gaikwad
Bharat Gaikwad

Reputation: 530

Each break statement terminates the enclosing switch statement. 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.

You can use if-then-else just, the overhead of testing is increases..., which is avoided in switch-case, but break is necessary for proper execution...

Upvotes: 0

Houp
Houp

Reputation: 145

It's correct behaviour. "default" doesn't mean all possible answers. It means all answers without these written in cases.

int x=1
switch(x)
    {
        default:
             System.out.println("no value found");
        case 1:
            System.out.println("1");
        case 2:
            System.out.println("2");
    }

will print

1
2

won't print

no value found
1
2

when using breaks; statements and default value there is called only one "branch" for each value

Upvotes: 0

AllTooSir
AllTooSir

Reputation: 49432

The switch statement is an abstraction of a branch table, and a branch table also has an implicit fall-through and requires an additional jump instruction to avoid it. Read this SO answer as well.

As per JLS 14.11:

The body of a switch statement is known as a switch block.

If one of the case constants is equal to the value of the expression, then we say that the case matches, and all statements after the matching case label in the switch block, if any, are executed in sequence.

If all these statements complete normally, or if there are no statements after the matching case label, then the entire switch statement completes normally.

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89189

The switch statement is not like an if-then-else statement. switch statement has multiple execution path.

The Documentation states:

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.

Upvotes: 0

Elbek
Elbek

Reputation: 3494

From java doc:

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

Upvotes: 0

j00lz
j00lz

Reputation: 281

It's to do with how it's compiled and optimization.

It's why a switch is preferable to a bunch of chained if statements.

Upvotes: 0

kiheru
kiheru

Reputation: 6618

switch is fall through. You need to add break before each next case unless you want that.

switch (x) {
    case 1:
        System.out.println("1");
        break;
    case 2:
        System.out.println("2");
        break;
    default:
        System.out.println("no value found");
}

Upvotes: 0

tbodt
tbodt

Reputation: 17007

For reasons that are inherited from C++, execution naturally proceeds until the end of switch or a break. There is no test against cases as they are encountered.

Upvotes: 0

Related Questions