You are on page 1of 8

CONDITIONAL switch STATEMENT

Introduction
• switch statements which are used to perform
only specific block of statements in a program
called Case Control Statements.
• It tests the value of a variable and compares it
with multiple cases. Once the case match is
found, a block of statements associated with
that particular case is executed [1].
FORMAT: Flowchart of
switch STATEMENT switch STATEMENT
switch (variable)
{
case 1: statement; break;
case 2: statement; break;
case 3: statement; break;
.
.
case 4: statement; break;
default: statement;
}
FORMATS:
switch STATEMENT
switch (variable) switch (variable)
{ {
case ‘a’: statement; break; case ‘+’: statement; break;
case ‘b’: statement; break; case ‘-’: statement; break;
case ‘c’: statement; break; case ‘*’: statement; break;
. .
. .
case ‘z’: statement; break; case ‘%’: statement; break;
default: statement; default: statement;
} }
Break
• Terminates the loop immediately when
encountered.
Example 1:
• Write a C++ program CONDITIONAL switch
STATEMENT that determine if the first
inputted number is greater or lesser or equal
to the second inputted number.
Example 2:
• Write a C++ program CONDITIONAL switch
STATEMENT that determine operator [+,-,*,/]
to be applied in getting the result of two
inputted numbers.
References:

[1] Guru99.com. 2020. Switch...Case Statement In C (Examples). [online] Available at:


<https://www.guru99.com/c-switch-case-statement.html#:~:text=Switch%20Case%20Example
%20in%20C&text=A%20switch%20construct%20is%20used,whose%20case%2Dlabel%20is
%208.> [Accessed 15 August 2020].

You might also like