![]() ![]() This behavior is different from if/ else conditional statements which execute only one block of code. Note: Without the break keyword at the end of each case, the program would execute the code for the first matching case and all subsequent cases, including the default code. In the code above, suppose grade is equal to 10, then the output would be “Sophomore”. It’s essentially the else part in an if/ else if/ else statement. If none of the cases are true, then the code in the default statement will run. At the end of each switch statement, there is a default statement. ![]() ![]() The break keyword tells the computer to exit the block and not execute any more code or check any other cases inside the code block.If the value of grade is equal to 9, then the code that follows the : would run. The case keyword checks if the expression matches the specified value that comes after it.SyntaxĪ switch statement looks like: switch (expression), there are multiple cases. There’s also an optional default statement marking code that executes if none of the case statements are true. The break keyword can be used to terminate a case. The default clause of a switch statement will be jumped to if no case matches the expression's value. If there is a match, the code within starts to execute. The switch statement evaluates an expression, matching the expression's value against a series of case clauses, and executes statements after the first case clause with a matching value, until a break statement is encountered. A switch statement provides a means of checking an expression against various case statements. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |