The default for Microsoft C is that the Microsoft extensions are enabled. ANSI C requires at least 257 case labels be allowed in a switch statement. The number is limited only by the available memory. Microsoft C doesn't limit the number of case values in a switch statement. In this example, if constant-expression equals any letter between 'a' and 'f', the convert_hex function is called. It's included for consistency.Ī single statement can carry multiple case labels, as the following example shows: switch( c ) The final break statement isn't strictly necessary, since control passes out of the body at the end of the compound statement. Similarly, if i is equal to 0, only z is incremented if i is equal to 1, only p is incremented. The break following the statement n++ causes execution control to pass out of the statement body, bypassing the remaining statements. If i is equal to -1, only n is incremented. The break statement forces an exit from the statement body after one statement is executed. In this example, a break statement follows each statement of the switch body. Only total is incremented when c doesn't equal 'A' or 'a'. If c is equal to 'a', letter_a and total are incremented. Execution control is transferred to the first statement ( capital_a++ ) and continues in order through the rest of the body. The following examples illustrate switch statements: switch( c )Īll three statements of the switch body in this example are executed if c is equal to 'A', since no break statement appears before the following case. The switch statement transfers control directly to an executable statement within the body, bypassing the lines that contain initializations. Any static variables are initialized before executing into any switch statements.ĭeclarations can appear at the head of the compound statement forming the switch body, but initializations included in the declarations are not performed. The case and default labels of the switch statement's body are significant only in the initial test that determines where execution starts in the statement body. The value of each case constant-expression must be unique within the statement body. The type of switch expression and case constant-expression must be integral. A case or default label can only appear inside a switch statement. It may appear anywhere in the body of the switch statement. The default statement doesn't have to come at the end. There can be at most one default statement. If there's no default statement, and no case match is found, none of the statements in the switch body get executed. The default statement is executed if no case constant-expression value is equal to the value of expression. This continuation may be desirable in some situations. Without break, the program continues to the next labeled statement, executing the statements until a break or the end of the statement is reached. It branches to the end of the switch statement. You can use the break statement to end processing of a particular labeled statement within the switch statement. statements executed if expression does not equal statements executed if the expression equals the Use of the switch statement usually looks something like this: switch ( expression ) Execution proceeds until the end of the body, or until a break statement transfers control out of the body. Execution of the switch statement body begins at the first statement in or after the matching labeled-statement. However, no two constant-expression values within the same switch statement can have the same value. The switch statement can include any number of case instances. A constant-expression must have an unambiguous constant integral value at compile time.Ĭontrol passes to the case statement whose constant-expression value matches the value of expression. The values of expression and each constant-expression must have an integral type. SyntaxĪ switch statement causes control to transfer to one labeled-statement in its statement body, depending on the value of expression. The switch statement transfers control to a statement within its body. The switch and case statements help control complex conditional and branching operations.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |