Posts

Showing posts with the label switch construct

switch statement in cplus plus

Image
The switch construct The switch statement is very similar to the if-else-if ladder of section statements. Here is its general form: switch (expression) { case constant_1: statement_1 or block_1; break; case constant_2: statement_2 or block_2; break; case constant_3: statement_3 or block_3; break; // More cases may follow default statement_X or block_X; } In the switch construct, as in the if-else-if construct, multiple possible blocks of code are being selected from, and only one of them will typically be executed. The presence of the keyword break allows the execution to come out of the switch statement. Omission of break takes control through the next case statements irrespective of whether the value of case matches or not. This can also be used constructively when same operation has to be performed on a number of cases. Each case or possible block of code is initiated by the appearance of the case reserved word, and is terminated by the appearance of the