By 25/02/2014 7 Comments

The Switch Statement in C++

The switch statement is also the decision making statement like if statement and if-else statement. The switch statement is used when the bunch of decisions depend on the same variable. It works as if and if-else statements but it is some what clearer and easily understandable at glance. The switch statement starts with the keyword switch (lower case) followed by the variable in the parentheses (). The switch statement takes the decision according to the values of the variable in the parenthesis. The whole body of the switch statement is enclosed by curly braces. Then the switch statement matches the values of the variable with the cases in its body. The keyword case followed by the choice and the colon makes the block of code below it to be executed if the value of the switch variable matches with the choice in the case. You can give multiple cases in the switch statement and the switch statement will try to match the value of the variable with the cases and if no case is matched the control is returned to the default. The key word default followed by the colon causes the block of code below it to be executed if the switch statement does not match any of the case. The break statement is used after the block of code in each case, which causes the control to be transferred out of the switch statement when ever the case executes its block of code.

Syntax of the Switch Statement

switch (ch)
{
case 1:
statement;
statement;
break;
case 2:
statement;
statement;
break;
case 3:
statement;
statement;
break;
default:
statement;
statement;
}

 

Flow Chart of the Switch Statement

Switch-Flow-Chart







Posted in: C++ Basics

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .