Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a java switch case statement execute multiple cases for negative int values?

I just run over something strange in my java code:

switch (result) {
            case 0:
                result_amount = 500;
            case 1:
                result_amount = 600;
            case -1:
                result_amount = 700;
    } 

result is from primitive type int. For value 1 case 1 and case -1 are executed.

Is this a normal switch case behaviour? If yes: why?

like image 258
Martin Miller Avatar asked Aug 12 '15 08:08

Martin Miller


People also ask

Can switch-case have multiple conditions in Java?

Switch Statement in Java A Java switch statement is a multiple-branch statement that executes one statement from multiple conditions. The switch statement successively checks the value of an expression with a list of integer (int, byte, short, long), character (char) constants, String (Since Java 7), or enum types.

Can we use negative values in switch-case?

Yes, this behavior is defined. The values are cast to size_t as you'd expect.

Can case in switch statement have multiple values?

The switch can includes multiple cases where each case represents a particular value. Code under particular case will be executed when case value is equal to the return value of switch expression. If none of the cases match with switch expression value then the default case will be executed.

Does switch statement execute all cases?

A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using the strict comparison, === ) and transfers control to that clause, executing all statements following that clause.

How do you switch cases in Java with multiple values?

Java Switch Statement | Switch Case Multiple Values example. A Java switch statement is matched case (condition) and execute statement for that. In the Switch statement, using passing value and then this value will go down the list of the case to find matched one. If There is not matched then it will return the default statement.

What is the break statement in switch case Java?

In Switch case Java, the break statement is optional. Even if you remove the break, the control of the program will flow to the next case. Let’s consider the following example. public class example { public static void main (String [] args) { /* * Switch statement starts here.

What is the default value of a switch case in Java?

The value of the Switch case should be of the same data type as the Switch case variable. For E.g. – if ‘x’ is of integer type in a “switch (x)”, then all the Switch cases should be of integer type. The Java break statements can be used (optional) to terminate the sequence of executables inside a case. The default statement is also optional.

What is a nested switch case in Java?

This is known as a nested switch case in java. As a switch case defines its block, the case constraints in the inner switch do not conflict with those in the outer switch. Here is a Java program to demonstrate nested switch case statement:


3 Answers

You need to use the break keyword after a case block:

switch (result) {
        case 0:
            result_amount = all_amounts[i];
        break;
        case 1:
            result_amount = all_amounts[i];
        break;
        case -1:
            result_amount = all_amounts[i+1];
} 

The switch statement will make a jump to the correct case tag, then execute all the code that follow, ignoring potential other case tags. You can consider the switch statement just like a goto one.

like image 180
dotvav Avatar answered Oct 04 '22 05:10

dotvav


Fall Through.

quoting from docs

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.

You have to add the break after each case.

          case 1:
          result_amount = 600;
          break; 
like image 44
Suresh Atta Avatar answered Oct 04 '22 06:10

Suresh Atta


Remember:

This is a mistake that almost every beginner will make. That's why I like C# more. It doesn't allow "fall-through".

What you did wrong is you fell-through the switch statement. Try using 0 as the value of result. It will go through all the cases. When a switch case finishes execution, the next case gets executed. That's why we need to add a break; statement for each case of the switch statement.

switch (result) {
            case 0:
                result_amount = 500;
                break;
            case 1:
                result_amount = 600;
                break;
            case -1:
                result_amount = 700;
                break;
} 

But sometimes we want fall-through. For example when we want to calculate number of days in a month:

switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        days = 31;
        break;
    case 2:
        days = 28;
    case 4:
    case 6:
    //Lots of code...
}
like image 39
Sweeper Avatar answered Oct 04 '22 05:10

Sweeper