0% found this document useful (0 votes)
98 views

Structure and Modular Programming

The document discusses structured programming and its basic control structures in C++. It introduces functions and outlines C++'s 7 basic control structures: sequence, if, if/else, switch, while, for, and do/while. It then presents 4 rules for developing algorithms using these control structures: 1) Start with the simplest flowchart, 2) Replace rectangles with sequences, 3) Replace rectangles with control structures, 4) Apply rules 2 and 3 repeatedly. The document also discusses modularity and using functions to solve problems in a modular way through stepwise refinement.

Uploaded by

mewoja1073
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Structure and Modular Programming

The document discusses structured programming and its basic control structures in C++. It introduces functions and outlines C++'s 7 basic control structures: sequence, if, if/else, switch, while, for, and do/while. It then presents 4 rules for developing algorithms using these control structures: 1) Start with the simplest flowchart, 2) Replace rectangles with sequences, 3) Replace rectangles with control structures, 4) Apply rules 2 and 3 repeatedly. The document also discusses modularity and using functions to solve problems in a modular way through stepwise refinement.

Uploaded by

mewoja1073
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Structured programming

summary & intro modularity


Recap flow of control (Ch. 2&3)
Introduce functions (Ch. 4&5)
Notation for
algorithms
l Example
pseudocode
notation (not
a “standard”),
and flowchart
symbols
(relatively
standard)
C++’s 7 basic control structures
l 1st is trivial: sequence structure
l 3 choices of selection structures:
– if
– if/else
– switch
l 3 choices of repetition structures:
– while
– for
– do/while
Sequence (it really is a structure)
if Selection Structure

T
?

F
if/else Selection Structure

F T
?
switch Selection Structure

T
? break

T
? break

.
.
.
while Iteration Structure

T
?

F
for Iteration Structure

initialize

T
? increment

F
do/while Iteration Structure

T
?
F
Notice rectangles in every one
Selection
Sequence
T
?

F
T
Iteration F
?
T

T
? break

F
initialize
T
? break

T T F
? increment ? .
.
F F .
Structure “rule” #1: start with the
simplest flowchart
l One rectangle
l A good (and widely
Very
applicable) example:
general; get some data, calculate
top-level and show some results
algorithm
l Really just a way to start;
clarifies the “big picture”
Rule #2: replace any rectangle by two
rectangles in sequence

Rule 2

l This “stacking rule” can apply


repeatedly: oneàtwo, twoàthree, …
For example:
1. Get data
2. Process
3. Show results
Rule #3: replace any rectangle by any
control structure
Any one of 7
Rule 3
choices in C++

l This “nesting rule” also applies repeatedly,


as each control structure has rectangles
l e.g., nest a while loop in an if structure:
if (n > 0)
while (i < n)
cout << i++;
Rule #4: apply rules #2 and #3
repeatedly, and in any order

l Stack, nest, stack, nest, nest, stack, … gets


more and more detailed as one proceeds
– Think of control structures as building blocks
that can be combined in two ways only.
– Captures the essence of stepwise refinement:
keep adding details as they arise
l And keep adding control structures as long as more
are needed
Modularity – another structured
programming idea
l Function = the simplest type of C++ module
l Idea: let modules solve problem parts – then
combine the parts to solve whole problems
– Abstraction benefits – details are hidden in a
module to reduce complexity of overall solution
– Reusability benefits – maybe use it many times
– Benefits of unit tests – be confident each of the
parts work properly
Using functions to solve problems
l Think: you might be able to directly translate an
algorithm into a series of function calls
mydata = getData();
results = process(mydata);
showResults(results);
l In turn, the function process() might do:
intermediateResult = calculate(x, y);
where calculate is another function, to perform a
difficult calculation involving x and y.
– … “top-down programming by stepwise refinement”

You might also like