![]() When the break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. ![]() A break statement is used inside a switch to terminate the statement sequence.Even if the switch case statement does not have the default statement, it would run without any error. An expression provided in a switch should result in constant value otherwise, it would not be valid.Essential Points about Switch Case Statements The values of each case must be the same datatype as of the variable. Note: The variable used in the switch case must be one of the following from short,īyte, int, char. Hence, writing a break in every case statement is essential as it takes us out of the switch case after the execution of that particular case which is the requirement of the question. It is essential to use break statement in all the cases except the default as if we don’t write break in each case statement all the following case gets executed.įor example, in the above code, if we don’t write a break in each case statement, we will get the following output. The value of a there is two hence case 2 gets executed over there Note: ![]() ![]() Explanation : In the above code, I have passed an in the switch statement.
0 Comments
Leave a Reply. |