Ict4 LG22
Ict4 LG22
Grade 10 - ICT 4
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
Lesson Proper
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
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.
import javax.swing.JOptionPane;
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
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.
import java.util.*;
Sample Run:
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)