0% found this document useful (0 votes)
5 views

Lesson-10-Control-Structures

Uploaded by

alilayaashaheena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lesson-10-Control-Structures

Uploaded by

alilayaashaheena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lesson 10: Control Structures (else-if statement, nested if

statement, and switch-case statement)

The else-if statement


The else-if statement is also known as the multi-way selection structure. Several
conditions are tested in this structure of else-if statement.

General Forms of an else-if statement


The else-if statement makes use of the Java keywords if and else.
if (condition1) if (condition1)
statement1; {
statement1;
else if (condition2) statement2;
statement2; }

else if (condition3) else if (condition2)


statement3; {
statement3;
else if (condition n) statement4;
statement; }
.
. else
else statement5;
statement n;

The general form on the left specifies that statement1 will only be executed if
condition1 evaluates true. If the evaluation of condition1 is false, the execution will jump to
test condition2. When the evaluation of condition2 is true, statement2 is executed; otherwise,
the execution will jump to test condition3 and so on.

It should be remembered that in an else-if statement; if none of the conditions


(condition1, condition2, …, condition n) are satisfied, the statement that follows else is
automatically executed.

The general form on the right, however, specifies a block of statements that are to be
executed when the condition is satisfied. In the general form on the right, the statements
(statement1 and statement2) bounded by the left and right curly brackets are executed when
condition1 is true. Otherwise, the execution will jump to test condition2. When the evaluation
is true, statement3 and statement4 are executed. Execution of statement5 will be performed
when none of the conditions (condition1 and condition2) is satisfied.

Given below is a program using the else-if statement that identifies if the entered
number is even, odd or zero.
Source Code: Output:
The nested if statement
The nested if statement is a conditional structure when conditions are nested, that is,
another condition is tested if the previous condition is satisfied. This can be a nest of if, if-
else, or else-if statement.

General Form of a nested if statement


if (condition1)
if (condition2)
statement1;
else
statement2;

else if (condition3)
if (condition4)
statement3;
else
statement4;

else
if (condition5)
statement5;
else
statement6;

The general form specifies that if condition1 evaluates true, testing of condition2 is
performed. If condition2 evaluates true, statement1 is executed; otherwise, the execution of
statement2 is performed.

If the evaluation of condition1 is false, the execution will jump to test condition3. If
condition3 is satisfied, testing of condition4 is executed. If condition4 evaluates true,
statement3 is executed; otherwise, execution of statement4 is performed.

Lastly, if the evaluation of condition3 is false, the statement after else is automatically
executed, which is the testing of condition4. If the evaluation of condition4 is true, statement5
is executed. Otherwise, the statement (statement6) after the last else is executed.

Given below is a program using the nested if statement that identifies if the entered
number is even, odd, or zero.
Source Code: Output:
The switch-case statement
The switch-case statement is an equivalent form of an else-if statement and is
therefore used to implement a multi-way selection structure. This selection structure does
not require an evaluation of relational and Boolean expression.

General Form of a switch-case statement


The switch-case statement makes use of the Java keywords switch, case, break,
and default.
switch (expression)
{
case value1:
statement1;
break;

case value2:
statement2;
break;

case valuen:
statementn;

default:
statement;
}

The expression inside the parentheses is first substituted or evaluated. Take note
that the data type of this expression should be int, short, long and char only. Similarly, the
data type of the case constant values, value1, value2, value3, …, valuen, shall be the same
with the data type of the expression.

The left curly bracket specifies the start of the switch-case structure. It is mandatory
to start the scope of the switch-case structure with this symbol. The value1 in the case
value1: is then tested against the value of the expression. If a match is found, meaning that
the value of expression equals value1, the statement (statement1), associated with this case,
it is executed. When the break statement is executed, the execution will jump out of the
switch structure. The use of break in every case is not mandatory, and is only required to
implement the correct logic of the program.

If a match is not found, meaning that the value of expression is not equal to value1,
the execution will jump to test the next case constant value, value2, against the value of the
expression. If a match is found, the statement (statement2) will be executed until the break
is found and so on. If none of the case constant values, valuen, matches with the value of
the expression, the statement under default is automatically executed. Similar to break, the
use of default is just optional and is only needed to implement the program’s correct logic.
The right curly bracket specifies the end point of the switch-case structure.
The program below is the implementation of a switch-case structure.
Source Code: Output:

Activity
TRUE OR FALSE. Write TRUE if the statement expresses a fact; if not, write FALSE. Write your
answer on the space provided before each number.

____________ 1. It should be remembered that in an else-if statement; if none of the


conditions are satisfied, the statement that follows else is
automatically executed.
____________ 2. Every if statement must be followed by an else of else-if statement.
____________ 3. An if statement code must be defined in two braces.
____________ 4. An else or else-if statement cannot exist alone without the if statement.
____________ 5. The nested if statement allows many conditions.
____________ 6. Based on the general form of a nested if statement, if condition1
evaluates true, testing of condition2 is performed. If condition2
evaluates true, statement1 is executed; otherwise, the execution of
statement2 is performed.
____________ 7. A switch-case statement can be used to compare values for high or
low.
____________ 8. It is allowed to use duplicate case constraints inside a switch-case
statement.
____________ 9. The switch-case statement is a selection structure that requires an
evaluation of relational and Boolean expression.
____________ 10. The use of break in a switch-case program in every case is not
mandatory, and is only required to implement the correct logic of the
program.

You might also like