Module 10.2 Nested and Switch Statement
Module 10.2 Nested and Switch Statement
Nested if and if-else Statements
This lesson introduces the nested if and if-else statements as well as switch
statement in JAVA. Specifically in this section, the use of one if or else if statement
inside another if or else if statement/s will be presented to demonstrate how to
select actions to be performed depending on several conditions. The use of
relational and logical operators that were introduced in the previous lesson will
also be presented. Another selection control statements aside from if and if-else is
the switch statement which will also be presented as an alternative method for
conditional statements. Problem sets (Try Me!) will be given at the end of the lesson
to give you time to practice before taking the assessment tasks. Assessment tasks
will be submitted on or before the scheduled date as specified in the Course
Guide.
Learning Outcomes:
At the end of the lesson, you are expected to:
evaluate the output of code fragments with nested if and if-else
statements, or switch statement
create programs using nested if and if-else statements, or switch
statements
Nested if and if-else Statements are statements that makes use of if or else
if statement inside another if or else if statement/s.
if (condition_1)
{
If (condition_2)
{ Outer if
Inner if
block_of_statements;
}
}
The inner if will be executed when condition_1 is true, otherwise the inner if
will be ignored. The block of statements in the inner if will be executed when
condition_2 is true, otherwise it will be completely ignored, too.
For example:
int x = 5;
int y = 10;
if (x == 5)
{
if (y == 10)
cout << “The value of x is 5 and the value of y is 10”;
}
When the code fragment above is evaluated, the string given in the cout
statement will be displayed since the first (x==5) and second (y==10) conditions
are true.
Take note that the operator used in the conditions is == and not =.
= is an Assignment Operator that is used to assign the value of variable or
expression, while == is an Equal to Operator and it is a relation operator used for
comparison.
On the other hand, the syntax for the nested if-else statement is:
if (condition_1)
{
If (condition_2)
{
block_of_statements;
} Inner
else if_else
{ Outer
if_else
block_of_statements;
}
}
else
{
block_of_statements;
}
The inner if-else will be executed when condition_1 is true, otherwise the else
statement will be executed.
For example:
if (n != 0)
{
if ((n % 2) == 0)
{
cout << "The number is even." << endl;
}
else
{
cout << "The number is odd." << endl;
}
}
else
{
cout << "The number is neither even nor odd." << endl;
}
When the code fragment above is evaluated and assuming that n is equal
to 4, the first condition (n!=0) becomes true. Hence, the inner if-else will be
executed. Substituting 4 to n, it becomes 4 % 2 which is equal to 0. Hence, the
second condition ((n % 2) == 0) becomes true and the number which is 4 is even.
B. switch Statement
switch (expression)
{
case constant_expression:
block_of_statements;
break;
case constant_expression:
block_of_statements;
break;
default:
block_of_statements;
}
where:
switch – is the keyword for the switch statement
expression – is the one that is evaluated and compared with the values of
each case
case – is the keyword for the case statement
constant_expression – must be a constant or literal that has the same data
type with the variable in the switch statement
break – is the keyword which terminates the switch statement and jumps to
the statement immediately following the switch.
default – is the keyword that is optional which is used when there is no match
from the given cases. The code after the default will then be
executed. There is no need to place a break in the default case.
For example:
char X;
switch (X)
{
case 'A':
cout << "Excellent";
break;
case 'B':
cout << "Very Good";
break;
case 'C':
cout << "Good";
break;
default:
cout << "Poor";
}
cout << "\n Done";
When the code fragment above is evaluated, let us assume that the value
of X is B. The first case will be tested. Since the value of the case which is A does
not match the value of the variable X, it goes to the second case. Since, the
second case matches the value of the variable which is B, then “Very Good” will
be displayed and jumps to the statement immediately following the switch
because of the break statement. The statement following the switch is the cout
statement, hence “Done” will be displayed.
Practice Exercises
1. Try completing the code fragments in the three given examples and check
the output for each.
2. Create a program using nested if-else that determines whether or not an
input grade is “very good”, “good”, and “poor” using the criteria below:
Grade
Very Good above 90
Good 89 – 83
Poor below 83
3. Create a program using switch statement given the same requirements as
in item #2.
To supplement the lesson in this module, you may visit the following links:
https://www.tutorialspoint.com/cplusplus/cpp_nested_if.htm
https://beginnersbook.com/2017/08/cpp-if-else-statement/
https://www.w3schools.com/cpp/cpp_switch.asp
https://www.youtube.com/watch?v=oMrt_87aW-U
https://www.youtube.com/watch?v=uvODOG3_hQk