Chapter 8 - Control Structures Flashcards
What are control statements?
means of producing non-sequential execution of program statements through selection of alternative paths or repeating statements
definition of a control structure?
a control statement and the collection of statements whose execution it controls
definition of a selection statement
provides a means of choosing between two or more execution paths in a program
What are the 2 categories of selection statements?
2-way selection multiple selection (n-way)
definition of compound statement
a group of statements controlled as a unit
The 4 design issues with selection
1) what is the form of the control expression
2) what is the selectable segment form (single statement, compound state, etc.)
3) how should the meaning of nested selectors be specified
4) how are the ‘then’ and ‘else’ clauses specified
Statement sequence vs a compound statement
statement sequence - a series of statements that are terminated with some reserved word
compound statement - see above. Surrounded by braces
The 4 PLs that have statement sequences in their then/else clauses instead compound statements
FORTRAN 95
Ada
Python
Ruby
The main issue that arises with nested selectors?
When a nested ‘if’ is present, there’s not a straightforward way to determine which a corresponding ‘else’ should be associated with
- C based languages resolve this by using {}
- using statement sequences resolves this ambiguity much more cleanly
5 design issues with multiple selection statements
1 & 2 the same as with a two-way selection
3) is execution flow through the structure restricted to include just a single selectable statement
4) how are case values specified
5) how should unrepresented selector expression values be handled
3 early multiple selector schemes in FORTRAN
arithmetic if
[if (arithmetic expression) value1, value2, valueN]
-does a ‘go to’ to the label corresponding to value_i
computed goto
[GOTO (value1, value2, valueN) ]
-does the same thing as arithmetic if
assigned GOTO
[Assign value to N GOTO N (val1, val2, valN)]
-does the same thing as above
4 key design choices of Pascal case statements
1) case expressions are of any ordinal type
2) segments (the code controlled by the case expression) can be a single or compound statement
3) construct is encapsulated
4) only one segment can be executed per execution of the case structure
5 key design choices in C switch statements
1) control expression can only be of integer type
2) selectable segments can be statement sequences, blocks, or compound statements
3) construct is encapsulated
4) any number of segments can be executed in one execution of the structure (if no ‘break’ keyword is provided then the code simply continues to execute sequentially)
5) a ‘default’ clause handles unrepresented values
definition of an iterative statement
a statement that causes another statement of collection of statements to be executed zero to many times
2 main design issues with loops
1) how is iteration controlled
2) where is the control mechanism in the loop