C++ Control Flow Statements 10

switch statement

* switch statements help us to apply multiple conditions related to the value of a variable.

* The variable used with the switch statement can be either an int or a single character (or an expression can also be given).

* Based on the value of the variable specific set of statements are executed

* Different conditions in switch statements are applied using the keyword “case”.

 

Syntax:

switch(value/variable/exp)
{
case value1:
Statements;
break;
case value2:
Statements;
break;
case value3:
Statements;
break;

Any number of cases

default:
Statements;
break;
}

* On execution of break statement the control jumps out of the switch block.
* If value of the variable does not match with any of the values specified with different cases then set of statements written in default clause get executed.