0% found this document useful (0 votes)
14 views4 pages

Ict4 LG22

This learning guide for Grade 10 ICT focuses on Selection Control Structures in Java, specifically if and if...else statements. It covers one-way and two-way selection with examples, explaining how to manipulate program flow based on conditions. The guide includes programming activities to reinforce understanding of these concepts.

Uploaded by

hamoudiguinal2
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)
14 views4 pages

Ict4 LG22

This learning guide for Grade 10 ICT focuses on Selection Control Structures in Java, specifically if and if...else statements. It covers one-way and two-way selection with examples, explaining how to manipulate program flow based on conditions. The guide includes programming activities to reinforce understanding of these concepts.

Uploaded by

hamoudiguinal2
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/ 4

Learning Guide

Grade 10 - ICT 4

Lesson 22 : Selection Control Structure (Part 1)

Pre-requisite Concept : Understanding the Building Blocks of Java

Time : 3 hours/week

Introduction

This learning guide will help you have a clear understanding on how to make
your program perform under certain given conditions and circumstances in the most
straightforward manner. This learning guide will discuss if and if…else statements.

Objectives

1. Manipulate conditions that control the program flow using if conditional


statement.
2. Use the different types of if statements in a simple Java program.

Lesson Proper

Selection: if and if…else

Although there are only two logical values, true and false, they are extremely
useful because they permit programs to incorporate decision making that alters the
processing flow.

One-Way Selection

A bank wants to send a notice to a customer if her or his checking account


balance falls below the required minimum balance. That is, if the balance is below the
required minimum, the bank should send a notice to the customer; otherwise, it should
do nothing. Similarly, if the policyholder of an insurance policy is a non-smoker, the
company wants to apply a 10% discount to the policy premium. Both of these
examples involve one-way selection. In Java, one-way selections are incorporated
using the if statement. The syntax of one-way selection is:

if (logical expression)
statement
Note the elements of this syntax. It begins with the reserved word if, followed by
logical expression contained within parentheses, followed by a statement. The
logical expression is also called a condition; it decides whether to execute the statement
that follows it. If logical expression is true, the statement executes. If it is false, the
statement does not execute and the computer goes on to the next statement in the
program.

The following Java program finds the absolute value of an integer.

import javax.swing.JOptionPane;

public class AbsoluteValue {

public static void main(String[] args) {


int number;
int temp;

String numString;

numString =
JOptionPane.showInputDialog("Enter an integer:");
number = Integer.parseInt(numString);
temp = number;

if (number < 0)
number = -number;

JOptionPane.showMessageDialog(null,
"The absolute value of " + temp
+ " is " + number,
"Absolute Value",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}

Sample Run:
Two-Way Selection

You learned how to implement one-way selections in a program. There are


many situations in which you must choose between two alternatives. For example, if a
part-time employee works overtime, the paycheck is calculated using the overtime
payment formula; otherwise, the paycheck is calculated using the regular formula. This
is an example of two-way selection. To choose between two alternatives – that is, to
implement two-way selections – Java provides the if…else statement. Two-way
selection uses the following syntax:

if (logical expression)
statement1
else
statement2

Take a moment to examine this syntax. It begins with the reserved word if, followed
by a logical expression contained within parentheses, followed by a statement,
followed by the reserved word else, followed by the second statement. Statements
1 and 2 can be any valid Java statements. In a two-way selection, if the value of the
logical expression is true, then statement1 executes. If the value of the logical
expression is false, then statement2 executes.

The following program determines an employee’s weekly wages. If the


hours worked exceed 40, then wages include overtime payment.

import java.util.*;

public class WeeklyWages {

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

public static void main(String[] args) {

double wages, rate, hours;

System.out.print("Line 2: Enter the working "


+ "hours: ");
hours = console.nextDouble();
System.out.println();

System.out.print("Line 5: Enter the pay " + "rate: ");


rate = console.nextDouble();
System.out.println();

if (hours > 40.0)


wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);
else
wages = hours * rate;
System.out.printf("Line 12: The wages are $%.2f %n",
wages);
System.out.println();
}
}

Sample Run:

Line 2: Enter the working hours: 60

Line 5: Enter the pay rate: 10

Line 12: The wages are $700.00

Activity

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 to get a number from the user and print whether it is positive or
negative. Use the Scanner class to get the input and
System.out.println/System.out.print to display the outputs.

2. Write a program that reads in two floating-point numbers and tests whether they
are the same up to three decimal places. Use the JOptionPane class for the
input and output.

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 (5 th 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