You are on page 1of 31

ITD 1213: OBJECT

ORIENTED PROGRAMMING

CHAPTER 4: (Part 1)
Control Structures
Objectives
Learn about decision making
Make decisions with the if and
if...else structures
Use multiple statements in an if or
if...else structure
Nest if and if...else statements
Use AND and OR operators

Java Programming, Fifth Edition 2


Objectives (continued)
Learn to make accurate and efficient decisions
Use the switch statement
Use the conditional and NOT operators
Understand precedence

Java Programming, Fifth Edition 3


Understanding Decision Making
Pseudocode
◦ Use paper and pencil
◦ Plan program’s logic
 By writing plain English statements
◦ Accomplish important steps in a given task
◦ Use everyday language
Flowchart
◦ Steps in diagram form
◦ Series of shapes connected by arrows

Java Programming, Fifth Edition 4


Understanding Decision Making
(continued)
Flowchart (continued)
◦ Programmers use variety of shapes to
represent different tasks
 Rectangle to represent any unconditional step
 Diamond to represent any decision
Sequence structure
◦ One step follows another unconditionally
◦ Cannot branch away or skip a step

Java Programming, Fifth Edition 5


Java Programming, Fifth Edition 6
Java Programming, Fifth Edition 7
Making Decisions with the
if and if...else Structures
if statement
◦ Simplest statement to make decision
◦ Boolean expression appears within
parentheses
◦ Space between keyword if and opening
parentheses
◦ Execution always continues to next
independent statement
◦ Use double equal sign (==) to determine
equivalency
Java Programming, Fifth Edition 8
Making Decisions with the
if and if...else Structures
(continued)

Java Programming, Fifth Edition 9


Pitfall: Misplacing a Semicolon in
an if Statement
No semicolon at end of first line of if
statement
◦ if (someVariable == 10)
◦ Statement does not end there
When semicolon follows if directly
◦ Empty statement contains only semicolon
◦ Execution continues with the next independent
statement

Java Programming, Fifth Edition 10


Pitfall: Misplacing a Semicolon in
an if Statement (continued)

Java Programming, Fifth Edition 11


Pitfall: Using the Assignment
Operator Instead of the Equivalency
Operator
Attempt to determine equivalency
◦ Using single equal sign
◦ Rather than double equal sign
◦ Illegal
Can store Boolean expression’s value in
Boolean variable
◦ Before using in if statement

Java Programming, Fifth Edition 12


Pitfall: Attempting to Compare
Objects Using the Relational
Operators
Usestandard relational operators to
compare values of primitive data types
◦ Not objects
Canuse equals and not equals
comparisons (== and !=) with objects
◦ To compare objects’ memory addresses
instead of values

Java Programming, Fifth Edition 13


The if...else Structure
Single-alternative if
◦ Only perform action, or not
 Based on one alternative
Dual-alternative if
◦ Two possible courses of action
if...else statement
◦ Performs one action when Boolean expression
evaluates true
◦ Performs different action when Boolean
expression evaluates false

Java Programming, Fifth Edition 14


The if...else Structure
(continued)
if...else statement (continued)
◦ Statement that executes when if is true or
false ends with semicolon
◦ Vertically align keyword if with the keyword
else
◦ Illegal to code else without if
◦ Depending on evaluation of Boolean expression
following if
 Only one resulting action takes place

Java Programming, Fifth Edition 15


The if...else Structure
(continued)

Java Programming, Fifth Edition 16


Using Multiple Statements
in an if or if...else Structure

Execute more than one statement


◦ Use pair of curly braces
 Place dependent statements within block
 Crucial to place curly braces correctly
Any variable declared within block local
to that block

Java Programming, Fifth Edition 17


Java Programming, Fifth Edition 18
Nesting if and if...else
Statements
Nested if statements
◦ Statements in which if structure is contained
inside of another if structure
◦ Use when two conditions must be met before
some action is taken
Paycareful attention to placement of
else clauses
else statements
◦ Always associated with if on “first in-last
out” basis

Java Programming, Fifth Edition 19


Nesting if and if...else
Statements (continued)

Java Programming, Fifth Edition 20


Using Logical AND and OR
Operators
Logical AND operator
◦ Alternative to some nested if statements
◦ Used between two Boolean expressions to
determine whether both are true
◦ Written as two ampersands (&&)
 Include complete Boolean expression on each side
◦ Both Boolean expressions that surround
operator
 Must be true before action in statement can occur

Java Programming, Fifth Edition 21


Using Logical AND and OR
Operators (continued)

Java Programming, Fifth Edition 22


Using Logical AND and OR
Operators (continued)
Logical OR operator
◦ Action to occur when at least one of two
conditions is true
◦ Written as ||
 Sometimes called pipes

Java Programming, Fifth Edition 23


Using Logical AND and OR
Operators (continued)

Java Programming, Fifth Edition 24


Using the switch Statement
switch statement
◦ Alternative to series of nested if statements
◦ Test single variable against series of exact
integer or character values

Java Programming, Fifth Edition 25


Using the switch Statement
(continued)
Switch structure keywords
◦ switch
 Starts structure
 Followed by test expression
 Enclosed in parentheses
◦ case
 Followed by one of the possible values for test
expression and colon

Java Programming, Fifth Edition 26


Using the switch Statement
(continued)
Switch structure keywords (continued)
◦ break
 Optionally terminates switch structure at end of each
case
◦ default
 Used prior to any action if test variable does not
match any case
 Optional

Java Programming, Fifth Edition 27


Using the switch Statement
(continued)

Java Programming, Fifth Edition 28


Using the switch Statement
(continued)
break statements in switch structure
◦ If break statement omitted
 Program finds match for test variable
 All statements within switch statement execute from
that point forward
case statement
◦ No need to write code for each case
◦ Evaluate char variables
 Ignore whether uppercase or lowercase

Java Programming, Fifth Edition 29


Using the switch Statement
(continued)
Why use switch statements?
◦ Convenient when several alternative courses of
action depend on single integer or character
variable
◦ Use only when there are reasonable number of
specific matching values to be tested

Java Programming, Fifth Edition 30


" When you are blessed with success, don't forget where you
came from and don't act brand new to the people that were
there for you.“
~Sybil Armstrong~

THANKS

You might also like