Switch Statement IN C/C++: by Quratulain Naqvi (Paki Tech)
Switch Statement IN C/C++: by Quratulain Naqvi (Paki Tech)
IN C/C++
By Quratulain Naqvi (Paki Tech)
Switch Statement
(by Paki Tech)
■ Switch case statements are a substitute for long if statements that compare a
variable to several integral values.
■ The switch statement is a multiway branch statement. It provides an easy way to
dispatch execution to different parts of code based on the value of the expression.
■ Switch is a control statement that allows a value to change control of execution.
Switch Statement
(by Paki Tech)
■ Syntax:
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
Important points about Switch Statement
(by Paki Tech)
a. The expression provided in the switch should result in a constant value otherwise it
would not be valid.
■ Valid expressions for switch:
// Constant expressions allowed switch(1+2+23) switch(1*2+3%4)
■ Flowchart:
Switch Statement
(by Paki Tech)