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?
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.
Yes, this behavior is defined. The values are cast to size_t as you'd expect.
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.
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.
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.
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.
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.
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:
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.
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;
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...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With