Unit 5. Structured Programming
Warm-up Task 1. Answer the following questions: 1. What is structured programming? 2. What structured programming languages do you know? 3. What are the main rules of structured programming? Reading Task 2. Read the text below to find answers to the following questions: |
- What was the approach to writing software in the early days of programming?
- Where does the term ‘spaghetti code’ come from?
- What are the five tenets of structured programming?
- What is the role of the control structure?
- What control structures are necessary to write programs?
- What does the sequence refer to?
- What levels of selection does C++ provide?
- What loop structures does C++ provide?
- What control structures are the most flexible and powerful for problem-solving?
- What control structures are the most common in C++ code?
Structured Programming
Up to this point in your study of computer science and C++, you have created programs which used only sequential execution. So far function main() consisted of a sequence of lines which are executed once, line-by-line. As we add the power of loops and selection, we need to use these tools in a disciplined manner.
In the early days of programming (1960's), the approach to writing software was relatively primitive and ineffective. Much of the code was written with goto statements which transferred program control to another part of the code. Tracing this type of code was an exercise in jumping from one spot to another, leaving behind a trail of lines similar to spaghetti. The term "spaghetti code" comes from trying to trace code linked together with goto statements.
The research of Bohm and Jacopini has led to the rules of structured programming. Here are five tenets of structured programming.
a. No goto statements are to be used in writing code.
b. All programs can be written in terms of three control structures: sequence, selection, and iteration.
c. Each control structure has one entrance point and one exit point. We will sometimes allow for multiple exit points from a control structure using the break statement.
d. Control structures may be stacked (sequenced) one after the other.
e. Control structures may be nested inside other control structures.
The control structures of C++ encourage structured programming. Staying within the guidelines of structured programming has led to great productivity gains in the field of software engineering.
There are only three necessary control structures needed to write programs: sequence, selection, and iteration.
Sequence refers to the line-by-line execution as used in your programs so far. The program enters the sequence, does each step, and exits the sequence.
Selection is the control structure allowing choice among different directions. C++ provides different levels of selection:
• One-way selection with an if structure
• Two-way selection with an if-else structure
• Multiple selection with a switch structure.
Iteration refers to looping. C++ provides three loop structures:
• while loops
• do-while loops
• for loops
Of the seven control structures, the if-else and while loop are the most flexible and powerful for problem-solving. The other control structures have their place, but if-else and while are the most common control structures used in C++ code.
Task 3. Complete the following text using the list of words below:
Back in the 1960s, computer programs were difficult to read. The 1) …… languages (FORTRAN and often even assembly languages) frequently used if and 2) …… statements, resulting in "spaghetti-like" 3) …… . Programs were essentially networks of statements, where the execution could 4) …… freely from one statement to another, using 5) …… or unconditional jump statements. This situation led the entire industry to use flow charts. The flow chart was a 6) …… which represented the program as a directed graph that connected 7) …… sections of the code. The execution could branch at the if statements, or could jump to any other 8) …… of the code, using the goto 9) ……. Flow charts helped programmers understand the 10) …… of their code.
statement conditional | primitive sequential | code goto | diagram section | logic jump |
Task 4. Work in pairs and decipher this limerick:
A programmer started to cuss
Because getting to sleep was a fuss
As he lay there in bed
Looping 'round in his head
was: while(!asleep()) sheep++;