Nested if-else statement
Nested if-else statement
By
Amjad Khan Khalil
[email protected]
Nested If-else statement
The simple if-else statement allows a choice to
be made between two possible alternatives,
but some time a choice must be made
between more than two possibilities. Then the
nested if-else statement will be used.
One “if” inside the body of another “if” or one
“else” inside the body of another “else” is
called nested if-else statement.
Also “if” can be nested inside the “else” part
and vise versa.
No limit that how deeper you nest the if-else
statements.
Con’t
The general Syntax will be as under:
if(condition1)
statement1;
else
if(condition2)
statement2;
else
if(condition3)
statement3;
else
statement4;
Con’t
Another example of nested if-else
if(condition1)
if(condition2)
statement1;
else
if(condition3)
statement2;
else
statement3;
Else
if(condition4)
statement4;
else
statement5;
Example
Write a program in C++ that
accept marks of a student in one
subject as input using nested if-
else and calculate that whether
the student is pass or fail and also
show its grade.
Drawbacks of nested if-
else
If the number of conditions increases then
the program creeps towards right and
some of the coding become invisible.
Care needs to be exercised while putting
the corresponding else part for an if part.
Care needs to be exercised while putting
the corresponding closing bracket for an
opening bracket.
Care needs to be exercised for the level
of indentation.
Switch statement
To remove the drawbacks of
nested if-else we use the switch
statement.
It is also a flow control structure
which allow the user to select a
single statement or block of
statements amongst the number
of available alternatives.
Con’t
The general syntax of switch statement is as under.
switch ( expression)
{
case 1:
statement1;
case 2:
statement2;
case 3:
statement3;
default:
statement4;
}
Points must be noted for switch statement
The expression in the parentheses may be int or char type.
The control will go to that case with the number of which the
expression matches.
Each case will consist of one or more statements.
If there are more than one statements in a case then no need of
enclosing them in curly brackets.
If the cases are represented by numbers then there must be an
integer expression inside switch.
If the cases are represented by characters then the switch statement
must produce a character value.
No float value is allowed to represent a case.
No mix expression is allowed to represent a case.
It is not necessary that the cases must be in proper sequence. It may
be in any order.
When control comes inside a case statement, it will execute the
statements of that cases and the statements of all subsequent next
cases.
If no match occurs with case then default case will be executed.
The break statement
The break statement takes the
control outside the body of the
switch statement or from the body
of loop.
Con’t
switch (expression)
{
case 1:
statement1;
break;
case 2:
statement2;
break;
case 3:
statement3;
break;
default:
statement4;
}
Advantages of switch()
The program doesn’t creep towards right.
No care requires for level of indentation
No care requires for putting corresponding
closing bracket for an opening bracket
No care needs for putting an “else” for
corresponding “if” statement.
The program is more readable with
switch() statement.