Learn C++ - The Switch Statement

The Switch statement is an easy to use alternative to the If Else Statement.This selection statement successively tests the value of an expression against a list of integer or character constants. When match of  integer or character is found then the statement associated with that constant is execueted .
Syntax of Switch statement is as follow :




switch(expression)
{
case 1: statement associated with 1;
break;

case 2: statement associated with 2;
break;

case 3: statement associated with 3;
break;
default: default statement;
break;
}


Explanation: switch () will check for the the match of the condition , and will execute the statement associated with matching case . The statement will be executed till it encounters Break statement.
If there is no break statement execution will continue to next case till it encounter break statement or en of switch statement.

Default statement get executed when no matching case is found. it is an optional statement. If default statement is missing and all matches fail then no action will take place