Flow Chart and Psuedo Code II
Flow Chart and Psuedo Code II
Flow Chart and Psuedo Code II
Introduction
Repetition control structures
while dowhile for
Counter-controlled repetition
Definite repetition; i.e., number of repetitions is known Control variable is used to count repetitions and incremented (or decremented) each time the group of instructions is performed
Sentinel-controlled repetition
Indefinite repetition; i.e., number of repetitions is not known in advance Sentinel value indicates end of data
It must be distinct from regular data items
Counter-controlled Repetition
Counter-controlled repetition requires
The name of a control variable (or loop counter) The initial value of the control variable The condition that tests for the final value of the control variable (when looping should terminate) The increment (or decrement) by which the control variable is modified each time through the loop
Code in Java:
int product = 2; while ( product <= 1000 ) { product = 2 * product; } Braces are not required if there is
only one statement in the body of the while structure, but it is good programming practice to use them
product is the control variable of this while loop Avoid using floating-point numbers as the control variable!
statement
Example:
product <= 1000 False True
In this example, when product equals 1024, condition will fail and while loop terminates
product = 2*product
Syntax:
do { statement } while ( condition );
Good programming practice to put braces to avoid misinterpreted as a while statement containing an empty statement Not placing a semicolon here would generate a syntax error!
Braces are not required if there is only one statement in the body of the do/while structure, but it is good programming practice to use them
What happens if you change the expression at last line of the code to while (counter++ <= 10);
1 2 3 4 5 6 7 8 9 10 11
True
Example:
print counter ++counter <= 10 True
False
Syntax:
statement
for (number = 2; number <= 100; number += 2) { System.out.print (number); Loop continuation condition }
Braces are not required if there is only one statement in the body of the for structure, but it is good programming practice to use them No semicolon after the last expression!
True
statement
expression3
Adjustment of the control variable is done at the end of the loop
Example:
Loopcontinuation test is done at the beginning of the loop
number = 2
Initialization of the control variable when the for structure begins executing (performed only once)
True print number
number += 2
False
Compare with:
for (number=2; number<=100; number+=2) { System.out.print( number + \t); }
Example:
int number = 2; while ( number <= 100 ) { System.out.print( number + \t); number += 2; }
System.out.print(i+j );
What is the output of this code?
Note the comma here
0 2 4 6 8 10
Increment (or decrement) expression in the for structure is tested at the end of the body of the loop, so postincrement (e.g., k++) and preincrement (e.g., ++k) have the same effect but usually postincrement is used to make it more natural.
Final notes
If loop-continuation condition is initially false
The body of the for structure is not executed Control proceeds with the next statement after the for structure
Control variables are usually used inside the body of the loop
Problem:
A class of ten students took a quiz. The grades (integers in the range of 0 to 100) for this quiz are available to you. Determine the class average on the quiz. Pseudocode:
initialize total to 0 Initialization of variables initialize counter to 1 Uninitialized variable while counter is less than or equal to 10 contains a garbage value. input next grade Using them will probably add grade into total produce incorrect results. increment counter by 1 This is an example of the set the average to the total divided by 10 non-fatal logic (or runtime) print the average error.
Body of the loop can consist of a single statement, compound statement, or even an empty statement; in the case of the compound statement, braces are required
True
counter++
Problem:
Generalize Example 1 to an unknown number of students Develop a class-averaging program that will process an arbitrary number of grades each time the program is run. How will the program knows when to end?
Using sentinel value; also known as signal value, dummy value, or flag value Indicates end of data entry Loop ends when user inputs the sentinel value Sentinel value must be chosen so that it cannot be confused with the regular input (such as 1 in this example)
Divide problem into smaller tasks and list them in order (first refinement):
initialize the variables input, sum and count the quiz grades calculate and print the average
Proceed to the next level of refinement -- commit to specific variables (second refinement):
Many programs can be divided logically into 3 phases: Initialization: Initializes the program variables Processing: Inputs data values and adjusts program variables accordingly Termination: Calculates and prints final results This helps the breakup of programs for top-down refinement
Refine the processing phase from input, sum and count the quiz grades to:
input the first grade (possibly the sentinel) while the user has not as yet entered the sentinel add this grade into the running total increment counter by 1 input the next grade (possibly the sentinel)
Refine the termination phase from calculate and print the average to: To prevent the
if the counter is not equal to 0 fatal divide by set the average to the total divided by the counter zero error! print the average else print No grades were entered."
Second refinement:
Refine initialize the variables to: initialize student counter to 1 initialize passes to 0 initialize failures to 0
Summary on Programming
Rules for programming
Developed by the programming community Only single-entry/single-exit control structures are used Rules:
1) Begin with the simplest flowchart 2) Stacking rule: Any rectangle (action) symbol can be replaced by two other rectangle (action) symbols in sequence 3) Nesting rule: Any rectangle (action) symbol can be replaced by any control structure (sequence, if, if/else, switch, while, do/while, for) 4) Rules 2 and 3 can be applied in any order and any number of times
Summary on programming
Rule 1: Begin with the simplest flowchart
Rule 2 (Stacking Rule): Any rectangle (action) symbol can be replaced by two rectangle symbols in sequence
Summary on programming
F T
Rule 3 (Nesting Rule): Any rectangle (action) symbol can be replaced by any control structure
T F
Summary on programming
Rules for programming
All programs can be broken down into 3 controls
Sequence: Handled automatically by the compiler Selection: if, if/else, or switch Repetition: while, do/while, or for
Any selection can be rewritten as an if statement, and any repetition can be rewritten as a while statement