Switch(b)is evaluated and it matches with 100/10 So, switch starts executing all the statements below case 25/5. Switch(a) is evaluated and it matches with case 25/5 If the default block is not written in the switch then control comes out of switch and no case block is executed. If no match is found between in switch and s in case, thenĬontrol goes to default block if it exists( default is optional ), If break is not present after the matching case statements are executed, it will continue to execute all the statements in the below cases including default, till the end of switch statement. If a break is present after the matching case statements are executed, control will come out of the switch So, as seen in the above example, the flow of control in a switch is determined by the presence of break inside the case: Which interrupted the flow and control came out of the switch block. This is because there is a break statement following case 5. The output of the switch would be: I am Five. (This is a weird output, if we gave 9 as input, we only want to print I am Nine as output, so we have to write break immediately after printf(“I am Nine”) to stop the flow)įor instance, If the a value is 5 instead of 9 in the above switch block. ![]() ![]() Since there are no break statements(break is optional), all the statements following case 9 are executed. All the statements following case 9 are executed until a break statement or end of switch are encountered. Explanation: The a variable value is 9, this is compared against the case values, and a match is found.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |