Title: Lecture 02 Object Oriented Programming
1Lecture 03Control Structure - Selection and
Repetition -
2Outline
- Selection
- if statement
- switch statement
- Repetition
- while statement
- for statement
3Control Structure
- A standard progression of logical steps to
control the sequence of statement execution - Basic control structures
- Sequence
- Selection
- Repetition
4Sequence Logical Control Structure
- This is the overriding control structure.
- Instructions are carried out in order or
sequence. - Action can be inputs, processes, or outputs
Action 1
Action 2
Action 3
5Selection Logical Control Structure I
- Instructions are executed depending on the
outcome of a test also called branching - If.then.else
Condition
Condition is FALSE
Condition is TRUE
Action 1
Action 2
6Decision Making
Looks Like rain
NO
YES
Wash car
Test cancelled
NO
YES
Study for test
7Relational Operators
lt
less than
lt
less than or equal to
gt
greater than
gt
greater than or equal to
equal to
!
not equal to
The boolean expression, e.g. (userResponse
Y), must appear within parentheses
8Logical Operator
- Use AND operator () within a boolean expression
to determine whether two expression are both
true. - Both boolean expression must be true before the
action in the statement can occur
if (itemSold gt 3 totalValue gt 1000) bonus
50
9Logical Operator
- Use OR operator () when you want some action ot
occur even if only one of two conditions are true
// using two ifs if (itemSold gt 100) bonus
50 else if (totalValue gt 1000) bonus 50
// Using OR operator If (itemSold gt 100
totalValue gt 3000) bonus 50
10if Structure
- One-way selection
- Making a decision involves choosing between two
alternative courses of action based on some value - The value upon which a decision is made is a
boolean value
boolean expression
Comparison operators (relational operator) are
results in boolean values
Resulting action
11The if Statement
- Branch only if the condition is True
- Format
- if (relational expression)
- block of statements to be executed if
- relational expression is true
-
12The if Statement The else Clause
- Two-way selection
- Branch in one direction if test is True, another
if False. - Format
- if (relational expression)
- block of statements executed when True
-
- else
- block of statements executed when False
-
13Example
if (studentGrade gt 60) System.out.println (
Passed) else System.out.println (Failed)
Grade gt 60
true
Print passed
false
14Compound (Block of) Statements
- The if and ifelse structures control only one
statement at a time. - A compound statement
- If you want to execute more than one statement if
the expression in an if or ifelse statement
evaluates to true. - E.g
- if (age gt 18)
- System.out.println (Eligible to vote)
- System.out.println (No longer .)
- else
- System.out.println (Not eligible to vote)
- System.out.println (Still..)
15The if Statement Nesting if Statements
- if and if-else statements can contain any valid
JAVA statements, including other if or if-else
statements. - Placing an if or if-else within an if or if-else
is called nesting. - When nesting if-else statements within on
another, care must be taken. The pairing of ifs
and elses can be tricky!
16If..else and ifelse if Statement
if (StudentGrade gt 90) System.out.println
(A) else if (StudentGrade gt 80)
System.out.println (B) else if
(StudentGrade gt 70)
System.out.println (C) else
if (StudentGrade gt 60)
System.out.println (D) else
System.out.println(F)
17Conditional Operator ?
- The conditional operator, ?, is a ternary
operator, which means that it takes three
arguments - Expression 1 ? Expression2 expression3
- If expression1 evaluates to true, the result of
the conditional expression is expression2.
Otherwise, the result of the conditional
expression is expression3 - E.g.
- if (a gt b)
- max a
- else
- max b
18Example Grade
19Selection Logical Control Structure II
- Case control structure
- Allow more than two choices when the condition
evaluated
Condition
Condition4 is TRUE
Conditio3 is TRUE
Conditio2 is TRUE
Conditio1 is TRUE
Action 1
Action 1
Action 1
Action 1
20The switch Statement
- An alternative to the series of nested if
statements - Uses four keywords
- switch starts the structure and is followed
immediately by a test expression enclosed in
parentheses. - case is followed by one of the possible values
for the test expression and a colon - break optionally terminates a switch structure
at the end of case - default optionally is used prior to any action
that should occur if the test variable does not
match any case
21The switch Statement
Switch (year) case 1 System.out.prinln
(Freshman) break case 2
System.out.prinln (Sophomore) break case
3 System.out.prinln (Junior) break
case 4 System.out.prinln (Senior)
break default System.out.println (Invalid
year)
if (year 1) System.out.println
(Freshman) else if (year 2)
System.out.println (Sophomore) else if (year
3) System.out.println (Junior) else if
(year 4) System.out.println
(Senior) else System.out.println(
Invalid year)
22The switch Statement
- break statement
- You can leave out the break statement in a switch
structure - When you omit the break, if the program find a
match for the test variable, the all statements
within the switch statement form that point
forward will execute
23Example Shipping Option
24The Repetition Structure
- Repetition
- Pre-Test Repetition
- Sentinel Value Loop
- Post-Test Repetition
- Fixed-Count Loop
- Nested Loops
25Repetition Logical Control Structure
- Allows for repeated execution of a set of
statements based upon the outcome of a condition
or test. - Also known as looping or iteration.
- Two components of a repetition structure
- Relational Expression or Test
- Loop Body
26Relational Expressions
- Relational expression
- Set up as for Selection.
- Always evaluates to either True (any non-zero
value) or False (a zero value). - Body is executed as long as the test is True.
- Exit the loop when test is False.
27Loop Body
- Is usually a block of code
- Contains instructions that reflect the task to be
repeatedly performed - Can contain any valid Java statements
- Usually includes code so that the test will
eventually prove False (or an infinite loop
condition will result).
28Pretest Repetition while Statement
- Condition is tested before the body is ever
executed. - Use a while loop to execute a body of statements
continuously while some condition continues to be
true.
Boolean expression
true
Loop body
false
29The while Statement
- while loop can be used to implement pretest
repetition - Format
- while (relational expression)
- statements executed while test
- is True
-
- Example
- While (4 gt2)
- System.out.println(hello)
30The while Statement
- To prevent an infinite while loop
- The loop control variable is initialized
- The loop control variable is tested in the while
statement - The body of the while statement must take some
action that alter the value of the loop control
variable - Example
- loopCount 1
- while (loopCount lt 3)
-
- System.out.println (Hello)
- loopCount loopCount 1
31Increment and Decrement Operators
- Increment Operators
- (all add 1 to the variable)
- int icount 0
- icount icount 1
- icount 1
- icount
- icount
- Decrement Operators
- (all subtract 1 from the variable)
- int dcounter 0
- dcounter dcounter - 1
- dcounter - 1
- dcounter--
- --dcounter
32Example
33Pretest Repetition the for Statement
- for loop is counted loop and can also be used to
implement pretest repetition - Use a while loop when you need to perform a task
some predetermined number of times - A loop that executes a specific number of time is
a counted loop - Format
- for(initialization expression altering)
- statements executed while condition
- is True
-
- Example
- for (int counter 1 counter lt10 counter)
Loop condition and final value
Control variable name and initial value
Increment of control variables
34Output Format
Pakage from java.text import
java.text.DecimalFormat
DecimalFormat precisionTwo new Decimal Format
(0.00)
Pakage from javax.swing
Number of columns
import javax.swing.JTextArea
JTextArea outputTextArea new JTextArea (11,20)
Number of rows
35Example The for Repetition
36Posttest Repetition The do-while Statement
- Condition is tested after the body is executed
- Body is always executed at least once
- Often used for data validation, where data is
validated as it is interactively entered.
37The do-while Statement
- do-while loop is used to implement posttest
repetition - Format
- do
- statements executed while
- condition is True
- while (relational expression)