Content Analysis
Object Oriented Design and Programming
Week 4
A B Emran Salahuddin (Sydney)
Manzur Ashraf (Melbourne)
Kent Institute Australia Pty. Ltd.
ABN 49 003 577 302 CRICOS Code: 00161E RTO Code: 90458 TEQSA Provider Number: PRV12051
Version 2 – 18th December 2015
1
SLIDE TITLE
Farrell, J. (2017) Programming Logic and Design, Comprehensive (9th ed.) Cengage Learning
2
2
Programming Logic and Design Ninth Edition
Chapter 5
Understanding Programming Structures
3
Programming Logic and Design, Ninth Edition
Objectives
Programming Structure-Looping
While loop
Do-while loop
For loop
Combination of programming structures
Understanding the Reasons for Structure
4
Programming Logic and Design, Ninth Edition
The Loop Structure
Loop structure
Repeats a set of actions while a condition remains true
Loop body
Also called repetition or iteration
Condition is tested first in the most common form of loop
The while…do or while loop or for loop
5
Programming Logic and Design, Ninth Edition
5
The Loop Structure (continued -2)
Loop structure
while testCondition continues to be true
do someProcess
endwhile
while you continue to be hungry
take another bite of food
determine if you still feel hungry
endwhile
6
Programming Logic and Design, Ninth Edition
6
The Loop Structure (continued -3)
while loop
Ask a question and, depending on the answer, you might or might not enter the loop to execute the loop’s procedure.
A while loop is also called a pretest loop because a condition is tested before entering the loop even once.
7
Programming Logic and Design, Ninth Edition
7
The Loop Structure (continued -4)
while loop
The important point to note when using while loop:
Use increment or decrement statement inside while loop
Loop variable gets changed on each iteration
And at some point condition returns false
This way we can end the execution of while loop otherwise the loop would execute indefinitely.
8
Programming Logic and Design, Ninth Edition
8
The Loop Structure (continued -5)
while loop in Java
Program to print 2-10 in reverse order using while loop
9
Programming Logic and Design, Ninth Edition
Loop starting point
Condition check
Decrement statement
9
The Loop Structure (continued -6)
do-while loop
Ensure that the procedure executes at least once; then, depending on the answer to the controlling question, the loop may or may not execute additional times.
Do-while and do-until loops are also called post test loops because a condition is tested after the loop body has executed.
10
Programming Logic and Design, Ninth Edition
10
The Loop Structure (continued -7)
do-while loop in Java
Program to print 2-10 in reverse order using do-while loop
11
Programming Logic and Design, Ninth Edition
Loop starting point
Condition check
Decrement statement
11
The Loop Structure (continued -7)
Finding sum of n numbers entered by user
You don’t know the exact number of inputs from user.
Use some sentinel value to end the program.
12
Programming Logic and Design, Ninth Edition
The Loop Structure (continued -8)
Loop structure
for( starting point , conditioncheck,increment/decrement)
Your statements here
Initialization happens first and only one time.
Condition in for loop is evaluated on each iteration.
if the condition is true then the statements inside for loop body gets executed.
Once the condition returns false, the statements in for loop does not execute
Control gets transferred to the next statement in the program after for loop.
13
Programming Logic and Design, Ninth Edition
13
The Loop Structure (continued -9)
for loop in Java
14
Programming Logic and Design, Ninth Edition
14
The Loop Structure (continued -7)
Finding sum of 10 numbers entered by user
You know the exact number of inputs from user.
15
Programming Logic and Design, Ninth Edition
The Loop Structure (continued -9)
Three loops have now been introduced the for, while and do-while
Use a for … loop
• If the number of repetitions can be determined prior to executing the loop i.e. it’s a counter controlled loop
Use a while … loop
• If the number of repetitions cannot be determined prior to executing AND
• Zero or more repetitions are possible
Use a do-while loop
• If the number of repetitions cannot be determined prior to executing AND
• One or more repetitions are possible
• This is a much rarer case than zero or more repetition
16
Programming Logic and Design, Ninth Edition
Combining Structures
All logic problems can be solved using only sequence, selection, and loop
Structures can be combined in an infinite number of ways
Stacking structures
Attaching structures end-to-end
End-structure statement
Indicates the end of a structure
The endif statement ends an if-then-else structure
The endwhile statement ends a loop structure
17
Programming Logic and Design, Ninth Edition
17
Combining Structures (continued -1)
18
Programming Logic and Design, Ninth Edition
18
Combining Structures (continued -2)
Any individual task or step in a structure can be replaced by a structure
Nesting structures
Placing one structure within another
Indent the nested structure’s statements
Block
A group of statements that execute as a single unit
19
Programming Logic and Design, Ninth Edition
19
Combining Structures (continued -3)
20
Programming Logic and Design, Ninth Edition
20
Combining Structures (continued -4)
21
Programming Logic and Design, Ninth Edition
21
Combining Structures (continued -5)
22
Programming Logic and Design, Ninth Edition
22
Combining Structures (continued -6)
23
Programming Logic and Design, Ninth Edition
23
Combining Structures (continued -7)
Structured programs have the following characteristics:
Include only combinations of the three basic structures
Each structure has a single entry point and a single exit point
Structures can be stacked or connected to one another only at their entry or exit points
Any structure can be nested within another structure
24
Programming Logic and Design, Ninth Edition
24
Understanding the Reasons for Structure
Use structured programming for:
Clarity—unstructured programs are confusing
Professionalism—other programmers expect it
Efficiency—most languages support it
Maintenance —other programmers find it easier to read
Modularity —easily broken down into modules
Structured programming is sometimes called goto-less programming
25
Programming Logic and Design, Ninth Edition
25
Recognizing Structure
26
Programming Logic and Design, Ninth Edition
26
Recognizing Structure (continued -1)
27
Programming Logic and Design, Ninth Edition
27
28
Programming Logic and Design, Ninth Edition
Recognizing Structure (continued -2)
28
29
Programming Logic and Design, Ninth Edition
Recognizing Structure (continued -3)
29
30
Programming Logic and Design, Ninth Edition
Recognizing Structure (continued -4)
30
Summary
Three loops
While loop is a pretest loop. Use it if the number of repetitions cannot be determined prior to executing
Do-while loop is post test loops. If the number of repetitions cannot be determined prior to executing AND One or more repetitions are possible
For loop is counter controlled loop. Use it if the number of repetitions can be determined prior to executing the loop
31
Programming Logic and Design, Ninth Edition
31
Summary (continued)
Structured techniques promote:
Clarity
Professionalism
Efficiency
Modularity
Logical steps can be rewritten to conform to the three structures: sequence, selection, and loops
32
Programming Logic and Design, Ninth Edition
32
kent.edu.au Kent Institute Australia Pty. Ltd. ABN 49 003 577 302 ● CRICOS Code: 00161E ● RTO Code: 90458 ● TEQSA Provider Number: PRV12051
33
33