Switch Statement and its Use
(switch statement c programming, switch statement c++, switch statement tutorial, switch statement example, switch statement syntax, switch statement as3, switch statement java example, switch statement in c language)
(switch statement c programming, switch statement c++, switch statement tutorial, switch statement example, switch statement syntax, switch statement as3, switch statement java example, switch statement in c language)
Switch Statement
Sometimes, we have
multiple conditions and take some action according to each condition. For
example, in the payroll of a company, there are many conditions to deduct tax
from the salary of an employee. If the salary is less than Rs. 10000, there is
no deduction. But if it falls in the slab Rs. 10000 – 20000, then the income
tax is deducted. If it exceeds the limit of Rs. 20000, some additional tax will
be deducted. So the appropriate deduction is made
according to the category or slab of the salary. We can also understand this
from the example of grades of a student. If the student has grade ‘A’ we print
‘Excellent’ and 'Very good', 'good', 'poor' and 'fail' for grades B, C, D, and
F respectively. Now we have to see how this multi-condition situation can be
applied in a program. We have a tool for decision making i.e. 'if statement'.
We can use 'if statement' to decide what description for a grade should
be displayed. So we check the grade in if statement and
display the appropriate description. We have five categories
of grades-- A, B, C, D, and F. We have to write five if statements to
check all the five possibilities (probabilities) of
grade. So we write this in our program as under-
if ( grade == ‘A’ )
cout <<
“Excellent” ;
if ( grade == ‘B’ )
cout << “Very
Good” ;
if ( grade ==
‘C’ )
cout << “Good” ;
if ( grade == ‘D’ )
cout << “Poor” ;
if ( grade == ‘F’ )
cout << “Fail” ;
These statements are correct and perform the
required task. In the following code we are using nested if statement.
If ( grade == ‘A’ )
cout
<< “Excellent” ;
else if ( grade == ‘B’ )
cout << “Very Good” ;
else if ( grade == ‘C’ )
cout
<< “Good” ;
else if ( grade == ‘D’ )
cout
<< “Poor” ;
else if ( grade == ‘F’ )
cout
<< “Fail” ;
In
the above example, we see that there are two approaches for a multi way
decision. In the first approach, we use as many if statements as needed.
This is an expensive approach. The second is the use of nested if statements.
The second is little more-efficient than the first one. In the 'nested if
statements' the nested else is not executed if the first if condition
is true and the control goes out of the if block. The C language
provides us a stand-alone construct to handle these instances. This construct is
switch structure. The switch structure is a multiple-selection
construct that is used in such cases (multi way decisions) to make the code
more efficient and easy to read and understand. The syntax of switch statement
is as follows.
switch ( variable/expression
)
{
case constant1 : statementLlist1 ;
case constant2 : statementLlist2 ;
case constantN : statementListN ;
default : statementList ;
}
In
the switch statement, there should be an integer variable (also include char)
or an expression which must evaluate an integer type (whole numbers only, the
decimal numbers 2.5, 14.3 etc are not allowed). We can’t use compound
conditions (i.e. the conditions that use logical operators && or ||) in
switch statement and in case statements. The constants also
must be integer constants (which include char). We can’t use a variable
name with the case key word. The default statement is optional. If there
is no case which matches the value of the switch statement, then
the statements of default are executed. The switch statement
takes the value of the variable, if there is an expression then
it evaluates the expression and after that looks for its value among the
case constants. If the value is found among the constants listed
in cases, the statements in that statementList are executed.
Otherwise, it does nothing. However if there is a default (which is optional),
the statements of default are executed.
Thus our
previous grade example will be written in switch statement as below.
switch (
grade )
{
case ‘A’ : cout
<< “Excellent” ;
case ‘B’ : cout << “Very Good” ;
case ‘C’ : cout << “Good” ;
case ‘D’ : cout << “Poor” ;
case ‘F’ : cout << “Fail” ;
}
We know that
C language is 'case sensitive'. In this language, ‘A’ is different from ‘a’.
Every character has a numeric value which is stored by the computer.. The
numeric value of a character is known as ASCII code of the character. The ASCII
code of small letters (a, b, c etc ) are different from ASCII code of capital
letters (A, B, C etc). We can use characters in switch statement as the
characters are represented as whole numbers inside the computers. Now we will
see how the use of ' the letter a' instead of 'A' can affect our program. We
want our program to be user- friendly. We don’t want to restrict the user to
enter the grade in capital letters only. So we have to handle both small and
capital letters in our program. Here comes the limitations of switch statement.
We can’t say in our statement like case ‘A’ or ‘a’ : statements ; We have to
make two separate cases so we write
case ‘A” :
case ‘a’ :
statements;
In the switch
statement, the cases fall through the case which is true. All the
statements after that case will be executed right down to the end of the
switch statement. This is very important to understand it. Let's suppose
that the user enters grade ‘B’. Now the case ‘A’ is skipped. Next case ‘B’
matches and statement cout << “Very Good” ; is executed. After
that, all the statements will be executed. So cout << “Good” ; cout
<< “Poor” ;and cout << “Fail” ; will be executed after
one another. We don’t want this to happen. We want that
when a case matches, then after executing its statement, the control should
jump out of switch statement leaving
the other cases. For this purpose we use key work break.
No comments:
Post a Comment