Why is the output of the code 14, not 8 : alpha=3+5=8?

public class Application4{

public static void main(String[] args) {
int alpha=3;
int beta=5;

switch (beta){
case 3:alpha=alpha+3;
case 4:alpha=alpha+4;
case 5:alpha=alpha+5;
default:alpha=alpha+6;

}

System.out.println(alpha);
}
}

I think you forgot a break; statement, so calculation falls through to the next step.

you have to put break; after each case like this:

int alpha=3;
int beta=5;
switch (beta){
case 3:alpha=alpha+3;
break;
case 4:alpha=alpha+4;
break;
case 5:alpha=alpha+5;
break;
default:alpha=alpha+6;
break;
}
System.out.println(alpha);

No this was on question paper, So as the previous code is not correct, does it skips to the last step?

The output of the code is 14, not 8, because the switch statement in the code is missing break statements.

In a switch statement, once a case is matched, the code will continue to execute the subsequent cases until a break statement is encountered. Without break statements, the code will "fall through" to the next case and execute the statements in that case as well.

In this code, the value of `beta` is 5. So, the code will match the case 5 and execute the statements inside that case:

```java
case 5: alpha = alpha + 5;
```

After this statement, the value of `alpha` will be 8 (3 + 5 = 8). However, since there is no break statement, the code will continue to execute the default case as well:

```java
default: alpha = alpha + 6;
```

This statement adds 6 to the current value of `alpha` (which is 8). So, the value of `alpha` becomes 14 (8 + 6 = 14).

To achieve the desired output of 8, you need to add break statements after each case in the switch statement. Here's the corrected code:

```java
public class Application4 {

public static void main(String[] args) {
int alpha = 3;
int beta = 5;

switch (beta) {
case 3:
alpha = alpha + 3;
break;
case 4:
alpha = alpha + 4;
break;
case 5:
alpha = alpha + 5;
break;
default:
alpha = alpha + 6;
break;

}

System.out.println(alpha);
}
}
```

With this corrected code, the output will be 8.