Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

SENIOR HIGH SCHOOL

Self-Learning Home Task


Technical-Vocational-Livelihood
ICT-Computer Programming
11
JAVA PROGRAMMING
Fourth Quarter
SLHT 1

Programming
Programming

Name:________________________________
Grade & Section: _____________________
LESSON : SELECTION: if and if… else
Objectives
 Use selection control structure if and if…else in a program

What I Know
Directions: Suppose that x, y, and z are int variables and x=10, y=15, and z=20,
Determine whether the following expressions evaluates to true or false.
1. !(x > 10)
2. x <= 5 | | y < 15
3. (x !=5) && (y != z)
4. x >= z | | (x + y >= z)
5. (x <= y – 2) && (y >= z) | | (z - 2 != 20)

What’s New
Activity:

What Is It
SELECTION: if and if… else
Java has three selection or branch control structures: if and if… else statements, and the
switch structure. This lesson will discuss how if and if… else statements can be used to create
one-way selection, two-way selection, and multiple selections.

PROBLEM 1:
 A bank wants to send 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.
What is the requirements in this problem?
What selection control structure should we use?
Syntax of one-way selection

if (logical expression)
statement

Note:
if is a reserved word
Logical expression is also called condition
if logical expression is true, the statement executes

if (balance < minimumbalance )


System.out.println(“Your balance is below the required minimum balance.”);

Sample Program
package belowminimumbalance;
import java.util.*;
public class belowminimum balance {
static Scanner console = new Scanner (System.in);
public static void main (String[] args) {
int account number;
double balance, minimumbalance;
System.out.println(“Enter account number: ” );
accountnumber = console.nextInt();
System.out.println();
System.out.println(“Enter the current balance: “);
balance = console.nextDouble();
System.out.println();
System.out.println(“Enter the minimum balance: “);
minimumbalance = console.nextDouble();
if (balance < minimumbalance)
System.out.printf(“(“Your balance is below the required minimum balance.”););
System.out.println();
}
}
PROBLEM 2:
A company will determine an employee’s weekly wages. If the number of hours worked is more
than 40 then wages is computed with overtime payment which is 1.5 * its rate.

Syntax of two-way selection

if (logical expression)
statement1
else
statement2

Note:
If the value of the logical expression is true, then statement1 is executed. If the value of logical
expression is false, then statement2 executes.

package weeklywages;
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.println(“Enter the working hours:“ );
hours = console.nextDouble();
System.out.println();
System.out.println(“Enter the pay rate: “);
rate = console.nextDouble();
System.out.println();
if (hours > 40.00)
wages = 40.00 * rate + 1.5 * rate * (hours-40.00);
else
wages = hours * rate;
System.out.printf(“The wage is %.2f %n”, wages);
System.out.println();
}
}
Compound/Block of Statements

 Executing more than one statement if the statement is true or false


{
statement1
statement2
.
.
.
statement
}

Note: If you have multiple subordinate statements, these must be a block enclosed in braces

if (booleanExpression)
{
statement1;
statement2;

statementn;
}
Practice exercises:
What is the output of the following program?

package exercise;
public class Exercise {
public static void main (String[] args) {
int myNum = 10;
int yourNum = 10;

if (yourNum % myNum == 3)
{
yourNum = 3;
myNum = 1;
}
else if (yourNum % myNum == 2)
{
yourNum = 2;
myNum = 2;
}
else
{
yourNum = 1;
myNum = 3;
}
System.out.printfln (myNu, + “ “ + yourNum);
}
}

ASSESSMENT:
Directions: Read each item and answer the following questions.
A. Suppose that score is an int variable. Consider the following if statements:
if (score >= 90)
System.out.println(“Discount = 10%”);

1. What is the output if the value of score is 95?


A. Discount = 10% B. Discount = 95 C. No answer
2. What is the output if the value of score is 85?
A. Discount = 10% B. Discount = 95 C. No answer
B. Suppose that score is an int variable. Consider the following if statements:
if (score <= 70)
System.out.println(“Grade is C.”);
else if (score > 74 && score < 85)
System.out.println(“Grade is B.”);
else
System.out.println(“Grade is A.”

3. What is the output if the value of score is 65?


A. Grade is A. B. Grade is B. C. Grade is C.
4. What is the output if the value of score is 80?
A. Grade is A. B. Grade is B. C. Grade is C.
5. What is the output if the value of score is 97?
A. Grade is A. B. Grade is B. C. Grade is C.
HANDS-ON:
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. Note: Use if…
else statement in creating the program. Note: import java.util.*; to access the class Scanner

Sample run:

You might also like