This will produce the following result − Output ("Your grade is " + grade) Ĭompile and run the above program using various command line arguments. if a case is satisfied and break statement is present then following cases are not checked. Based on value of grade, each case is checked. In this example, we're showing use of switch statement where cases are based on a char. The default case can be used for performing a task when none of the cases is true. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.Ī switch statement can have an optional default case, which must appear at the end of the switch. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. Each case is followed by the value to be compared to and a colon. You can have any number of case statements within a switch. The variable used in a switch statement can only be integers, convertable integers (byte, short, char), strings and enums. The following rules apply to a switch statement − You can have any number of case statements. The syntax of enhanced switch statement is − Each value is called a case, and the variable being switched on is checked for each case. A switch statement allows a variable to be tested for equality against a list of values.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |