Branching - If Review

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

▪Prepare ½ sheet of

paper crosswise
Activator–Complete the following statements:

▪ If I do my friends work, then



▪ If I do not take a bath, then …
▪ If I eat and _________, then …
▪ If I love my parents, then…
▪ If my allowance is more than
1000, then I will _____.
Decisions in Real Life
▪ A decision is a statement that says that if something
is true, then something will happen as a
consequence.

For example:
▪If I do the dishes, then I will get my allowance.
▪If Pat gets a 90 percent or above, he will get an A.
▪If you will marry me, then you are so very lucky.
▪ Decisions are modeled in computers.

For example:
if (grade > =75)
str=“You Passed!”;
Relational Expressions
▪A relational operator is used to compare two values,
resulting in a relational expression.

For example:
number > 16
grade == ‘F’
passing >= 60
▪A list of relational operators:

< less than


> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to
Java Programming: From Problem
Analysis to Program Design, 4e

Chapter 4
Control Structures I: Selection
Consider the following source code, what is the
display output?

int x=0,y=0;
if(x>0) {
y=10;
}
else{
y=20;
}
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 6
Consider the following source code, what is the
display output?

int x=0,y=0;
if(x>0) {

}
y=10;

else{

}
y=20;
20
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 7
Consider the following source code, what is the
display output?

int x=-10,y=0;
if(x>10) {
y=10;
}
else{
y=20;
}
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 8
Consider the following source code, what is the
display output?

int x=-10,y=0;
if(x>10) {

}
y=10;

else{

}
y=20;
20
System.out.print(y);
Java Programming: From Problem Analysis to Program Design, 4e 9
Consider the following source code, what is the
display output?
int x=5,y=0;
if(x>10)
y=10;
else if(x<10)
y=50;
else{
y=5;
}
System.out.print(y);
10
Consider the following source code, what is the
display output?
int x=5,y=0;
if(x>10)

50
y=10;
else if(x<10)
y=50;
else{
y=5;
}
System.out.print(y);
11
Consider the following source code, what is the
display output?
int a=0;
String b=“”;
if(a<0) {
b=“POSITIVE”;
}
else if(a>0){
b=“NEGATIVE”;
}
else{
b=“POSITIVE”;
}
System.out.print(b);
12
Consider the following source code, what is the
display output?
int a=0;
String b=“”;
if(a<0) {
b=“POSITIVE”;
}
else if(a>0){
POSITIVE
b=“NEGAVTIVE”;
}
else{
b=“POSITIVE”;
}
System.out.print(b);
13
Consider the following source code, what is the
display output?
int a=-5,m=10,n=15,b=20,c=0;
String b=“”;
if(a<0) {
b=“NEGATIVE”;
}
else if(a>0){
b=“POSITIVE”;
}
else{
b=“ORIGIN”;
}
System.out.print(n); 14
Consider the following source code, what is the
display output?
int a=-5,m=10,n=15,b=20,c=0;
String b=“”;
if(a<0) {

}
b=“NEGATIVE”;

else if(a>0){
b=“POSITIVE”;
15
}
else{
b=“ORIGIN”;
}
System.out.print(n); 15
Chapter Objectives
• Learn about control structures
• Examine relational and logical operators
• Explore how to form and evaluate logical
(Boolean) expressions
• Learn how to use the selection control
structures if, if…else, and switch in
a program

Java Programming: From Problem Analysis to Program Design, 4e 16


Control Structures
• Three methods of processing a program
– 1.
– 2.
– 3.

Java Programming: From Problem Analysis to Program Design, 4e 17


Control Structures
• Three methods of processing a program
– In sequence
– Branching
– Looping
• Branch: altering the flow of program
execution by making a selection or choice
• Loop: altering the flow of program
execution by repetition of statement(s)
Java Programming: From Problem Analysis to Program Design, 4e 18
Flow of Execution

Java Programming: From Problem Analysis to Program Design, 4e 19


Question:

– Allows you to make comparisons in a program


– expression that has a value of either true or false

Java Programming: From Problem Analysis to Program Design, 4e 20


Relational Operators
• Relational operator
– Allows you to make comparisons in a program
– Binary operator
• Condition is represented by a logical
expression in Java
• Logical expression: expression that has a
value of either true or false

Java Programming: From Problem Analysis to Program Design, 4e 21


Relational Operators in Java

1.

2.

Java Programming: From Problem Analysis to Program Design, 4e 22


Relational Operators in Java

Java Programming: From Problem Analysis to Program Design, 4e 23


Relational Operators and
Primitive Data Types
• Can be used with integral and floating-point
data types
• Can be used with the char data type
• Unicode collating sequence

Java Programming: From Problem Analysis to Program Design, 4e 24


Java Programming: From Problem Analysis to Program Design, 4e 25
Java Programming: From Problem Analysis to Program Design, 4e 26
Relational Operators and the
Unicode Collating Sequence

Java Programming: From Problem Analysis to Program Design, 4e 27


Logical (Boolean) Operators

1.

2.

3.

Java Programming: From Problem Analysis to Program Design, 4e 28


Logical (Boolean) Operators

Java Programming: From Problem Analysis to Program Design, 4e 29


Logical (Boolean) Operators
(continued)

Java Programming: From Problem Analysis to Program Design, 4e 30


Logical (Boolean) Operators
(continued)

! (4<10)
! (‘a’= = ‘A’)
! (10!=10)

Java Programming: From Problem Analysis to Program Design, 4e 31


Logical (Boolean) Operators
(continued)
! (4<10) False
! (‘a’= = ‘A’) True
! (10!=10) True

Java Programming: From Problem Analysis to Program Design, 4e 32


Logical (Boolean) Operators
(continued)

1.

2.

Java Programming: From Problem Analysis to Program Design, 4e 33


Logical (Boolean) Operators
(continued)

Java Programming: From Problem Analysis to Program Design, 4e 34


Logical (Boolean) Operators
(continued)
Result

1.

2.

3.

4.

Java Programming: From Problem Analysis to Program Design, 4e 35


Logical (Boolean) Operators
(continued)

Java Programming: From Problem Analysis to Program Design, 4e 36


Precedence of Operators

Java Programming: From Problem Analysis to Program Design, 4e 37


Precedence of Operators
(continued)

Java Programming: From Problem Analysis to Program Design, 4e 38


Precedence of Operators
(continued)
(1<5) && (4!=4) || (5==5)

(1+2+3! = 5 || 5<5) && (9>6) || !(5-5= = 7)

(!( 1!=2) && (6%3+2 >=5%2))

&&

(( (4==4) || (3!=2) || (3<=4)))


Java Programming: From Problem Analysis to Program Design, 4e 39
Precedence of Operators
(continued)
(1<5) && (4!=4) || (5==5) 0

(1+2+3! = 5 || 5<5) && (9>6) || !(5-5= = 7) 1

(!( 1!=2) && (6%3+2 >=5%2))

&& 0
((4==4) || (3!=2) || (3<=4))
Java Programming: From Problem Analysis to Program Design, 4e 40
Short-Circuit Evaluation

• Definition: a process in which the computer


evaluates a logical expression from left to
right and stops as soon as the value of the
expression is known

Java Programming: From Problem Analysis to Program Design, 4e 41


Selection

• One-way selection
• Two-way selection
• Compound (block of) statements
• Multiple selections (nested if)
• Conditional operator
• switch structures

Java Programming: From Problem Analysis to Program Design, 4e 42


One-Way Selection

• Syntax
if (expression)
statement
• Expression referred to as decision maker
• Statement referred to as action statement

Java Programming: From Problem Analysis to Program Design, 4e 43


One-Way Selection (continued)

Java Programming: From Problem Analysis to Program Design, 4e 44


One-Way Selection (continued)
Example 4-10
//Program to determine 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:"); //Line 1
number = Integer.parseInt(numString); //Line 2
temp = number; //Line 3

Java Programming: From Problem Analysis to Program Design, 4e 45


One-Way Selection (continued)

if (number < 0) //Line 4


number = -number; //Line 5

JOptionPane.showMessageDialog(null,
"The absolute value of " + temp
+ " is " + number,
"Absolute Value",
JOptionPane.INFORMATION_MESSAGE); //Line 6
System.exit(0);
}

Java Programming: From Problem Analysis to Program Design, 4e 46


Two-Way Selection
• Give the Syntax:

Java Programming: From Problem Analysis to Program Design, 4e 47


Two-Way Selection
• Syntax
if (expression)
statement1
else
statement2
• else statement must be paired with an if

Java Programming: From Problem Analysis to Program Design, 4e 48


Two-Way Selection (continued)

Java Programming: From Problem Analysis to Program Design, 4e 49


Two-Way Selection (continued)
Example 4-14

if (hours > 40.0)


wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);
else
wages = hours * rate;

Question: if hours=50.5, what is the


value of wages?

Java Programming: From Problem Analysis to Program Design, 4e 50


Two-Way Selection (continued)
Example 4-14

if (hours > 40.0) 8,362.5


wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);
else
wages = hours * rate;

Question: if hours=50.5, rate=150, what


is the value of wages?

Java Programming: From Problem Analysis to Program Design, 4e 51


Two-Way Selection (continued)
Example 4-14 (continued)

if (hours > 40.0); //Line 1


wages = 40.0 * rate +
1.5 * rate * (hours - 40.0); //Line 2
else //Line 3
wages = hours * rate; //Line 4

• Because a semicolon follows the closing parenthesis of the if


statement (Line 1), the else statement stands alone
• The semicolon at the end of the if statement (see Line 1) ends the
if statement, so the statement at Line 2 separates the else clause
from the if statement; that is, else is by itself
• Since there is no separate else statement in Java, this code
generates a syntax error
Java Programming: From Problem Analysis to Program Design, 4e 52
Below is an example of what
Selection?
if (age > 18)
{
System.out.println("Eligible to vote.");
System.out.println("No longer a minor.");
}
else
{
System.out.println("Not eligible to vote.");
System.out.println("Still a minor.");
}

Java Programming: From Problem Analysis to Program Design, 4e 53


Compound (Block of) Statements
(continued)
if (age > 18)
{
System.out.println("Eligible to vote.");
System.out.println("No longer a minor.");
}
else
{
System.out.println("Not eligible to vote.");
System.out.println("Still a minor.");
}

Java Programming: From Problem Analysis to Program Design, 4e 54


Compound (Block of) Statements
• Syntax

Java Programming: From Problem Analysis to Program Design, 4e 55


Multiple Selection: Nested if
• Syntax • Else associated with
most recent incomplete
if (expression1) if
statement1
else if (expression2)
• Multiple if statements
statement2 can be used in place of
else if…else statements
statement3 • May take longer to
evaluate
Java Programming: From Problem Analysis to Program Design, 4e 56
Conditional (? :) Operator
• Ternary operator
• Syntax
expression1 ? expression2 : expression3

• If expression1 = true, then the result of the


condition is expression 2; otherwise, the
result of the condition is expression 3

Java Programming: From Problem Analysis to Program Design, 4e 57


Conditional (? :) Operator
int num=4;
String str=“”;

num < 0 ? str=“-” : str=“+”;

What is the value of str?

Java Programming: From Problem Analysis to Program Design, 4e 58


Conditional (? :) Operator
int num=4;
String str=“”;

num < 0 ? str=“-” : str=“+”; +


What is the value of str?

Java Programming: From Problem Analysis to Program Design, 4e 59


switch Structures

Java Programming: From Problem Analysis to Program Design, 4e 60


switch Structures (continued)
• In Java, switch, case, break, and default are
reserved words
• In a switch structure, the expression is evaluated
first
• The value of the expression is then used to perform
the actions specified in the statements that follow
the reserved word case
• The expression is usually an identifier
• The value of the identifier or the expression can be
only of type int, byte, short, or char

Java Programming: From Problem Analysis to Program Design, 4e 61


switch Structures (continued)
• The expression is sometimes called the
selector; its value determines which
statements are selected for execution
• A particular case value must appear only
once
• One or more statements may follow a case
label, so you do not need to use braces to
turn multiple statements into a single
compound statement
Java Programming: From Problem Analysis to Program Design, 4e 62
switch Structures (continued)
• The break statement may or may not appear after
each statements1, statements2, ...,
statementsn
• A switch structure may or may not have the
default label

Java Programming: From Problem Analysis to Program Design, 4e 63


switch Structures (continued)

Java Programming: From Problem Analysis to Program Design, 4e 64


switch Structures (continued)

Java Programming: From Problem Analysis to Program Design, 4e 65


switch Structures (continued)
Example 4-23
switch (grade)
{
case 'A':
System.out.println("The grade is A.");
break;
case 'B':
System.out.println("The grade is B.");
break;
case 'C':
System.out.println("The grade is C.");
break;

Java Programming: From Problem Analysis to Program Design, 4e 66


switch Structures (continued)
case 'D':
System.out.println("The grade is D.");
break;
case 'F':
System.out.println("The grade is F.");
break;
default:
System.out.println("The grade is invalid.");
}

Java Programming: From Problem Analysis to Program Design, 4e 67


Programming Example:
Cable Company Billing
• Input: customer’s account number,
customer code, number of premium
channels to which customer subscribes,
number of basic service connections (in
case of business customers)
• Output: customer’s account number and the
billing amount

Java Programming: From Problem Analysis to Program Design, 4e 68


Programming Example:
Cable Company Billing (continued)
• Solution
– Prompt user for information
– Use switch statements based on customer’s type
– Use an if statement nested within a switch statement
to determine amount due by each customer

Java Programming: From Problem Analysis to Program Design, 4e 69


Java Programming: From Problem Analysis to Program Design, 4e 70
Comparing Strings
• class String
– Method compareTo
– Method equals
• Given string str1 and str2
an integer  0 if string str1 str2

str1.compareTo(str2)= 0 if string str1is equal to string str2
an integer  0 if string str1  str2

Java Programming: From Problem Analysis to Program Design, 4e 71
Comparing Strings (continued)
String str1 = "Hello";
String str2 = "Hi";
String str3 = "Air";
String str4 = "Bill";
String str5 = "Bigger";

Java Programming: From Problem Analysis to Program Design, 4e 72


Comparing Strings (continued)

Java Programming: From Problem Analysis to Program Design, 4e 73


Comparing Strings (continued)

Java Programming: From Problem Analysis to Program Design, 4e 74


Chapter Summary

• Control structures are used to process programs


• Logical expressions and order of precedence of
operators are used in expressions
• If statements
• if…else statements
• switch structures
• Proper syntax for using control statements
• Compare strings
Java Programming: From Problem Analysis to Program Design, 4e 75

You might also like