Date post: | 08-Jan-2018 |
Category: |
Documents |
Upload: | eric-mcdonald |
View: | 228 times |
Download: | 3 times |
Essentials of Counter-Controlled Repetition
• Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable Increment/decrement of control variable through each loop Condition that tests for the final value of the control variable
Figure 5.1: WhileCounter.java
for Repetition Statement
• Handles counter-controlled-repetition details
for ( int counter = 1; counter <= 10; counter++ )
Increment of control variable
Control variable
Final value of control variable for which the condition is true
for keyword
Loop-continuation condition
Initial value of control variable
Required semicolon separator
Required semicolon separator
Fig. 5.3 for statement header components.
Figure 5.2: ForCounter.java
for ( initialization;loopContinuationCondition; increment ) statement;
can usually be rewritten as:
initialization;while ( loopContinuationCondition ) { statement; increment;}
For Statement
[counter <= 10]
[counter > 10]
int counter = 1
counter++Determine whether the final value of control variable has been reached
System.out.printf(“%d “, counter);
Establish initial value of control variable
Display the counter value
Increment the control variable
Fig. 5.4 for statement activity diagram.
Examples Using the for Statement
• Varying control variable in for statement Vary control variable from 1 to 100 in increments of 1
• For ( int i = 1; i <= 100; i++ ) Vary control variable from 100 to 1 in increments of –1
• for ( int i = 100; i >= 1; i-- ) Vary control variable from 7 to 77 in increments of 7
• for ( int i = 7; i <= 77; i += 7 )
Figure 5.5: Sum.java
Figure 5.6: Interest.java
do…while Repetition Statement
• do…while structure Similar to while structure Tests loop-continuation after performing body of loop
▴ i.e., loop body always executes at least once
action state
[true]
[false]
condition
Fig. 5.8 do…while repetition statement activity diagram.
Figure 5.7: DoWhileTest.java
switch Multiple-Selection Statement
• switch statement Used for multiple
selections instead of nested and/or multiple if-else statements
case a action(s) break
default action(s)
[true]
case b action(s) break
case z action(s) break
.
.
.
[false]
case a
[true]
[true]
case b
case z
[false]
[false]
Fig. 5.10 switch multiple-selection statement activity diagram with break statements.
Figure 5.9: SwitchTest.java
break and continue Statements
• break/continue Alter flow of control
• break statement Causes immediate exit from control structure
▴ Used in while, for, do…while or switch statements
• continue statement Skips remaining statements in loop body Proceeds to next iteration
▴ Used in while, for or do…while statements
Figure 5.11: BreakTest.java
Figure 5.12: ContinueTest.java
Labeled break and continue Statements
• Labeled block Set of statements enclosed by {} Preceded by a label
• Labeled break statement Exit from nested control structures Proceeds to end of specified labeled block
• Labeled continue statement Skips remaining statements in nested-loop body Proceeds to beginning of specified labeled block
Figure 5.13: BreakLabelTest.java
Figure 5.14: ContinueLabelTest.java
Logical Operators
• Logical operators Allows for forming more complex conditions Combines simple conditions
• Java logical operators && (conditional AND) & (boolean logical AND) || (conditional OR) | (boolean logical inclusive OR) ^ (boolean logical exclusive OR) ! (logical NOT)
Logical Operators
expression1 expression2 expression1 &&expression2
f al se f al se f al sef al se t r ue f al set r ue f al se f al set r ue t r ue t r ueFig. 5.15 && (conditional AND) operator truth table.
expression1 expression2 expression1 | |expression2
f al se f al se f al sef al se t r ue t r uet r ue f al se t r uet r ue t r ue t r ueFig. 5.16 | | (conditional OR) operator truth table.
Logical Operators
expression1 expression2 expression1 ^expression2
f al se f al se f al sef al se t r ue t r uet r ue f al se t r uet r ue t r ue f al seFig. 5.17 ̂(boolean logical exclusive OR) operator truth
table.
expression !expressionf al se t r uet r ue f al seFig. 5.18 ! (logical negation, or logical NOT) operator
truth table.
Figure 5.19: LogicalOperators.java
Operator Precedence
Operators Associativity Type++ - - right to left unary postfix++ - - + - !( t ype)
right to left unary
* / % left to right multiplicative+ - left to right additive< <= > >= left to right relational== ! = left to right equality& left to right boolean logical AND^ left to right boolean logical exclusive OR| left to right boolean logical inclusive OR&& left to right conditional AND| | left to right conditional OR?: right to left conditional= += - = *= / = %= right to left assignmentFig. 5.20 Precedence/associativity of the operators discussed so far.
Structured Programming Summary
• Sequence structures “built-in” to Java
• Selection structures If if…else switch
• Repetition structures While do…while for
[t]
[f]
[f]
[t]
break
break
[t]break
[t]
[f][t]
[f]
[t]
[f]
[t]
[f]
Repetitionwhile statement do while statement for statement
SelectionSequence
if else statement (double selection)
if statement (single selection)
switch statement (multiple selection)
.
.
.
[t][f]
default
Structured Programming Summary
Rules for Forming Structured Programs1) Begin with the “simplest activity diagram” (Fig. 5.23).2) Any action state can be replaced by two action states in sequence.3) Any action state can be replaced by any control statement (sequence,
i f , i f el se, swi t ch, whi l e, do whi l e or f or ).4) Rules 2 and 3 can be applied as often as you like and in any order.Fig. 5.22 Rules for forming structured programs.
action state
Fig. 5.23 Simplest activity diagram.
Structured Programming Summary
.
.
.action state
action state
apply Rule 2
apply Rule 2
apply Rule 2
action state
action state
action state
action state
action state
action state
Fig. 5.24 Repeatedly applying rule 2 of Fig. 5.22 to the simplest activity diagram.
Structured Programming Summary
action state
apply Rule 3
apply Rule 3
apply Rule 3 action stateaction state
action stateaction state action stateaction state
[f] [t]
[f] [t]
[f] [t][f] [t]
Fig. 5.25 Applying rule 3 of Fig. 5.22 to the simplest activity diagram.
Identifying Objects’ States and Activities
• State Describes an object’s condition at a given time
• Statechart diagram (UML) Express how an object can change state Express under what conditions an object can change state Diagram notation (Fig. 5.28)
▴ States are represented by rounded rectangles e.g., “Not Pressed” and “Pressed”
▴ Solid circle (with attached arrowhead) designates initial state▴ Arrows represent transitions (state changes)
Objects change state in response to messages• e.g., “buttonPressed” and “buttonReset”
Statechart Diagram
buttonReset
buttonPressed
PressedNot pressed
Fig. 5.27 Statechart diagram for FloorButton and ElevatorButton objects.
Identifying Objects’ States and Activities
• Activity diagram (UML) Models an object’s workflow during program execution Models the actions that an object will perform Diagram notation (Fig. 5.28)
▴ Activities are represented by ovals▴ Solid circle designates initial activity▴ Arrows represents transitions between activities▴ Small diamond represents branch
Next transition at branch is based on guard condition
Person Activity Diagram
[floor door closed]
press elevator button
enter elevator
move toward floor button
wait for door to open
press floor button
wait for door to open
[floor door open]
exit elevator
wait for passenger to exit elevator
[passenger on elevator][no passenger on
elevator]
Fig. 5.28 Activity diagram for a Person object.
Elevator Activity Diagram
close elevator door
ring bell
reset elevator button
[elevator idle][button on destination floor
pressed]
open elevator door
[elevator moving]
[button on current floorpressed]
[floor button pressed]
[elevator button pressed]
[summoned][not summoned]
set summoned to true
set summoned to false
move to destination floor
[button on destination floorpressed]
[button on current floorpressed]
Fig. 5.29 Activity diagram for the Elevator object.