Professional Documents
Culture Documents
JAVA 4th QTR SLHT1
JAVA 4th QTR SLHT1
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
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.
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
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%”);
Sample run: