0% found this document useful (0 votes)
29 views8 pages

Ict4 LG23

This document provides a learning guide about selection control structures in Java programming. It discusses compound statements, nested if statements, and switch structures. It includes examples of using these concepts to control program flow based on different conditions.

Uploaded by

phoebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views8 pages

Ict4 LG23

This document provides a learning guide about selection control structures in Java programming. It discusses compound statements, nested if statements, and switch structures. It includes examples of using these concepts to control program flow based on different conditions.

Uploaded by

phoebe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Learning Guide

Grade 10 - ICT 4

Lesson 23 : Selection Control Structure (Part 2)

Pre-requisite Concept : Understanding the Building Blocks of Java

Time : 3 hours/week

Introduction

This learning guide is a continuation on selection control structure. This learning


guide will discuss compound (block of) statements, nested if statements and switch
structure.

Objectives

1. Manipulate conditions that control the program flow using nested if statement
and switch structure.
2. Use nested if and switch statements in a simple Java program.

Lesson Proper

Compound (Block of) Statements

The if and if…else structures select only one statement at a time. Suppose,
however, that you want to execute more than one statement if the logical expression in
an if or if…else statement evaluates to true. To permit more complex statements, Java
provides a structure called a compound statement or a block of statements. A
compound statement takes the following form:

Statement1
Statement2
.
.
.
Statement
}
That is, compound statement or block consists of a sequence of statements enclosed in
braces. In an if or if…else structure, a compound statement functions as if it were a
single statement. thus, instead of having a simple two-way selection similar to the
following code:

if (age > 18)


System.out.println(“Eligible to vote.”);
else
System.out.println(“Not eligible to vote.”);

you could include compound statements, similar to the following code:

if (age > 18)


{
System.out.println(“Eligible to vote.”);
System.out.println(“No longer a minor.”);
}
else
{
System.out.println(“Not eligible to vote.”);
System.out.println(“Still a minor.”);
}

Multiple Selections: Nested if

In the previous learning guide, you learned how to implement one-way and two-
way selections in a program. However, some problems require the implementation of
more than two alternatives. For example, suppose that if the checking account balace
is greater than or equal to $50000, the interest rate is 5%; if the balance is greater
than or equal to $25000 and less than $50000, the interest rate is 4%; if the balance is
greater than or equal to $1000 and less than $25000. The interest rate is 3%;
otherwise, the interest rate is 0%. This particular problem has four alternatives – that
is, multiple selection paths. You can include multiple selection paths in a program by
using an if…else structure – if the action statement itself is an if or if…else statement.
When one control statement is located within another, it is said to be nested.

Example 1

Suppose that balance and interestRate are variables of type double. The following
statements determine the interestRate depending on the value of balance:

if (balance >= 50000)


interestRate = 0.05;
else if (balance >= 25000)
interestRate = 0.04;

elseif (balance >= 1000)


interestRate = 0.03;
else
interestRate = 0.00;
Example 2

Assume that score is a variable of type int. Based on the value of score, the following
code determines the grade:

if (score >= 90)


System.out.println(“The grade is A”);
else if (score >= 80)
System.out.println(“The grade is B”);
else if(score >= 70)
System.out.println(“The grade is C”);
else if(score >= 60)
System.out.println(“The grade is D”);
else
System.out.println(“The grade is F”);

The following examples will further help you see the various ways in which you can use
nested if structures to implement multiple selection.

Example 3

Assume that all variables are properly declared, and consider the following statements:

if (temperature >= 50) //Line1


if (temperature >= 80) //Line2
System.out.println(“Good day for swimming.”); //Line3
else //Line4
System.out.println(“Good day for golfing.”); //Line5
else //Line6
System.out.println(“Good day to play tennis.”); //Line7

In this Java code, the else in Line4 is paired with the if in Line2, and the else in Line6 is
paired with the if in Line1. Note that the else in Line4 cannot be paired with the if in
Line1. If you pair the else in Line4 with the if in Line1, the if in Line2 becomes the
action statement part of the if in Line1, leaving the else in Line6 dangling. Also, the
statements in Lines2 through 5 form the statement part of the if in Line 1.

Example 4

Assume that all variables are properly declared, and consider the following statements:

if (temperature >= 60) //Line1


if (temperature >= 80) //Line2
System.out.println(“Good day for swimming.”); //Line3
else //Line4
System.out.println(“Good day for golfing.”); //Line5

In this code, the else in Line4 is paired with the if in Line2. Note that for the else I
Line4, the most recent incomplete if is the if in Line2. In this code, the if in Line1 has
no else and is a one-way selection.
switch Structures

Recall that there are three selection, or branch, structures in Java. The two-
selection structure, which is implemented with if and if…else statements, usually
requires the evaluation of a (logical) expression. The third selection structure, which
does ot require the evaluation of a logical expression, is called a switch structure.
Java’s switch structure gives the computer the power to choose from many alternatives.

The general syntax of a switch statement is:

switch (expression)
{
case value1:
statements1
break;
case value2:
statements1
break;
.
.
.
case valuen:
statementsn
break;

default:
statements
}

In Java, switch, case, break, and default are reserved words. In a switch structure, the
expression is evaluated first. The value of the expression is then used to perform the
actions specified in the statements that follow the reserved word case.

Although it need not be, the expression is usually an identifier. Whether it is an


identifier or an expression, the value of the identifier of the expression can only be of
type int, byte, short, or char. The expression is sometimes called the selector. Its
value determines which statements may follow a case label, so you do not need to used
braces to turn multiple statements into a single compound statement. The break
statement may or may not appear after each statement1, statement2, …, statement. a
switch structure may or may not have the default label.

A switch statement executes according to the following rules:

1. When the value of the expression is matched against a case value (also called a
label), the statements execute until either a break statement is found or the end
of the switch structure is reached.
2. If the value of the expression does not match any of the case values, the
statements following the default label execute. If the switch structure has no
default label, and if the value of the expression does not match any of the case
values, the entire switch statement is skipped.
3. A break statement causes an immediate exit from the switch structure.

Example 1

Consider the following statements (assume that grade is a char variable):

switch (grade)
{
case „A‟:
System.out.println(“The grade is A.”);
break;
case „B‟:
System.out.println(“The grade is B.”);
break;
case „C‟:
System.out.println(“The grade is C.”);
break;
case „D‟:
System.out.println(“The grade is D.”);
break;
case „F‟:
System.out.println(“The grade is F.”);
break;
default:
System.out.println(“The grade is invalid.”);
}

In this example, the expression in the switch statement is a variable identifier. The
variable grade is of type char, which is an integral type. The valid values of grade are
‘A’, ‘B’, ‘C’, ‘D’, and ‘F’. Each case label specifies a different action to take, depending
on the value of grade. If the value of grade is ‘A’, the output is:

The grade is A.

Example 2

The following program illustrates the effect of the break statement. It asks the user to
input a number between 0 and 10.

import java.util.*;

public class BreakStatementsInSwitch {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) {


int num;

System.out.print("Enter an integer between " + "0 and


10: ");
num = console.nextInt();
System.out.println();

System.out.println("The number you entered " + "is " +


num);

switch(num)
{
case 0:
case 1:
System.out.print("Hello ");
case 2:
System.out.print("there ");
case 3:
System.out.print("I am ");
case 4:
System.out.println("Mickey.");
break;

case 5:
System.out.print("How ");
case 6:
case 7:
case 8:
System.out.println("are you?");
break;

case 9:
break;

case 10:
System.out.println("Have a nice day.");
break;
default:
System.out.println("Sorry the number is " + "out
of range.");
}
System.out.println("Out of switch " + "structure.");
}

Sample Run 1:

Enter an integer between 0 and 10: 0

The number you entered is 0


Hello there I am Mickey.
Out of switch structure.
Sample Run 2:

Enter an integer between 0 and 10: 3

The number you entered is 3


I am Mickey.
Out of switch structure.

Sample Run 3:

Enter an integer between 0 and 10: 4

The number you entered is 4


Mickey.
Out of switch structure.

Sample Run 4:

Enter an integer between 0 and 10: 7

The number you entered is 7


are you?
Out of switch structure.

Sample Run 5:

Enter an integer between 0 and 10: 9

The number you entered is 9


Out of switch structure.

Activity 23

Answer the following problems. Write your answers in a one whole sheet of paper.
Don’t forget to write your name, grade level, section and activity number.

1. Write a program that prompts the user to input a number. The program should
then output the number and a message saying whether the number is positive,
negative, or zero. Use the JOptionPane class for the input and output.

2. Write a program that prompts the user to input three numbers. The program
should then output the numbers in nondescending order. Use the Scanner class
to get the input and System.out.println/System.out.print to display the outputs.
Reminders

Do the activity and submit it on April 20, 2022.

References
1. Computer Advantage: Understanding Structured Programming (Second
Edition), 2009 By Teresita P. Pasadas.et. al.
(Textbook)
2. Java Programming: From Problem Analysis to Program Design (5th Edition),
2012 By D. S. Malik
3. Developing Useful Computer Applications for Today’s World Fundamentals of
Web Application Development II (2008)
By Dennis Cesar R. Patiño
4. Introduction to Computer by Antonio M. Andres, Sr. (2003)

You might also like