0% found this document useful (0 votes)
11 views

Lecture-04 Selection: by Dr. Bharati Mishra

Uploaded by

Johny Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture-04 Selection: by Dr. Bharati Mishra

Uploaded by

Johny Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Lecture-04

Selection
By
Dr. Bharati Mishra
Agenda
• boolean Data Type
• if Statements
• Two-Way if-else Statements
• Nested if
• Multi-Way if-else Statements
• Common Errors and Pitfalls
• Generating Random Numbers
• Case Study:
• Computing Body Mass Index,
• Computing Taxes
• Logical Operators
• Case Study:
• Determining Leap Year, Lottery
boolean Data Type

• Another primitive data type in Java


• Only has two values
• true
• false
• Examples
• boolean test = false;
• boolean checked = true;
• Usually used for checking conditions
Comparison Operators

• Six comparison operators in Java

Operator Name
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
Class vs. Primitive Type

• Primitive data type


• Predefined range
• A primitive variable stores the value
• Reference data type
• Class, Array [will be discussed later]
• Size is not predefined!
• stores a “reference” to the actual object

int x = 10; Integer y = new Integer(10);

10 Integer
….
y ….
x
Comparison Operators

• Be careful about ==
• Primitive data type:
• Compares the value
• Reference data type:
• Compares the references (memory address)!
• So only use with primitive data types
• E.g. if x and y are String
• x==y will not always return the correct result!
• Instead use String.compare(x,y)
• In general, use equals() to compare two objects
• E.g. a.equals(b)
Comparison Operators

• Result of comparison is boolean


• boolean x = (3 > 2);
• x is true
• boolean y = (1 > 2);
• y is false
Program
• Create a program to let a first grader practice
additions. The program randomly generates two
single-digit integers number1 and number2 and
displays a question such as:

“What is 7 + 9?”

to the student. After the student types the answer,


the program displays a message to indicate whether
the answer is true or false.
“if” statement
One way if
• Checks for a simple condition
• It is written as
if (boolean-expression) {
statement(s);
}

• E.g.
if (radius >= 0) {
area = radius * radius * PI;
}
One way if
• Be careful!
If Program
• Write a program that prompts the user to enter an
integer. If the number is a multiple of 5, print
HiFive. If the number is divisible by 2, print HiEven.
Two-way if
• If you get here by 10:00 am, then we can have
brunch, else we can go for lunch.

if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
Two-way if
• Example

if (radius >= 0) {
area = radius * radius * 3.14159;
System.out.println("The area is: “ + area);
}
else {
System.out.println("Negative input");
}
Multiple if-else
• else if is used for checking multiple conditions

if (score >= 90.0) if (score >= 90.0)


grade = 'A'; grade = 'A';
else if (score >= 80.0) else
grade = 'B'; if (score >= 80.0)
else if (score >= 70.0) grade = 'B';
grade = 'C'; else
else if (score >= 60.0) if (score >= 70.0)
grade = 'D'; grade = 'C';
else else
grade = 'F'; if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Trace
• Let’s trace a program!

Suppose score is 70.0


The condition is false

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Trace
• Let’s trace a program!

Suppose score is 70.0

if (score >= 90.0) The condition is false


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Trace
• Let’s trace a program!

Suppose score is 70.0

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0) The condition is true!
grade = 'B';
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Trace
• Let’s trace a program!

Suppose score is 70.0

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B'; The grade is C!
else if (score >= 70.0)
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Trace
• Let’s trace a program!

Suppose score is 70.0

if (score >= 90.0)


grade = 'A';
else if (score >= 80.0)
grade = 'B';
else if (score >= 70.0) exit.
grade = 'C';
else if (score >= 60.0)
grade = 'D';
else
grade = 'F';
Caution on if-else

• Note! The else clause matches the most recent if


clause in the same block.

• What is the printed on screen?


• Nothing!
Caution on if-else

• Nothing is printed from the preceding statement.


To force the else clause to match the first if
clause, you must add a pair of braces:
int i = 1;
int j = 2;
int k = 3;
if (i > j) {
if (i > k)
System.out.println("A");
}
else
System.out.println("B");

• This prints B.
Tips

• Some tips
Program

• This time program randomly generates two


single-digit integers number1 and number2 such
that (number1 > number2) and displays a
question such as

“What is 9 – 2?” to the student.


Logical Operators
Logical Operators

• What if we need more complex conditions


composed of “and/or/..”?

Operator Name
! not
&& and
|| OR
^ Exclusive OR
Logical Operators

• Not (!) operator


Logical Operators

• And (&&) operator


Logical Operators

• Or (||) operator
Logical Operators

• Exclusive Or (^) operator


Program

• A program that checks whether a number is


• divisible by 2 and 3
• divisible by 2 or 3
• divisible by 2 or 3 but not both.
Common Errors

• Adding a semicolon at the end of an if clause is a


common mistake.
if (radius >= 0); Wrong
{
area = radius*radius*PI;
System.out.println( "The area for the circle of radius "
+ radius + " is " + area);
}

• Equivalent to
if (radius >= 0) { };
other statements
• What is the output?
• Always prints the area, even for negative numbers
Case Study-01

• Write a program that randomly generates a lottery


of a two-digit number, prompts the user to enter a
two-digit number, and determines whether the
user wins according to the following rule:
• If the user input matches the lottery in exact
order, the award is $10,000.
• If the user input matches the lottery, the award is
$3,000.
• If one digit in the user input matches a digit in the
lottery, the award is $1,000.
Case Study -02
The United States federal personal income tax is calculated based on filing status and taxable
income. There are four filing statuses: single filers, married filing jointly or qualified
widow(er), married filing separately, and head of household. The tax rates vary every year.
Table below shows the rates for 2009. If you are, say, single with a taxable income of $10,000,
the first $8,350 is taxed at 10% and the other $1,650 is taxed at 15%, so, your total tax is
$1,082.50.
Case Study -03 : Determining Leap Year
•A year is a leap year if it is divisible by 4 but not
by 100, or if it is divisible by 400.

You might also like