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
2 main type of loops
1) count controlled loop- execute loop body a specified number of times
2) event controlled loop-execute loop body until some event occurs
2 types of iteration control
pretest (before the loop body)
posttest (after the loop body)
4 design issues with count-controlled loops
We know count-controlled loops use a counter variable to track the number loop body executions
1) what is the type and scope of the loop (counter) variable
2) what is the value of the loop (counter) variable at loop termination
3) should it be legal to modify the loop (counter) variable or loop parameters from within the body of the loop and does this affect loop control
4) should loop parameters be evaluated only once, or once for every iteration
4 count-controlled loop design choices in FORTRAN
1) loop variable can be an integer, real, or double
2) loop variable has its last value upon loop termination
3) loop parameters are evaluated only once
4) loop variable CANNOT be changed. Loop parameters (ie. stepsize) CAN be changed but because of 3) they DO NOT affect loop control
4 count-controlled loop design choices in Pascal
1) loop variable must be an ordinal type of usual scope
2) loop variable becomes undefined upon loop termination
3) loop parameters evaluated just once
4) loop variable CANNOT be changed. Loop parameters CAN be changed but because of 3) they DO NOT affect loop control
4 count-controlled loop design choices in Ada
1) the loop variable is implicitly declared by specifying the range and its type is that of the range
2) loop variable does not exist outside of loop (completely destroyed on loop termination)
3) the range is evaluated just once
4) loop variable CANNOT be changed. Loop range CAN be changed but because of 3) it DOES NOT affect loop control
4 count-controlled loop design choices in C (pre C99)
AKA a for-loop
1) no explicit loop variable
2) everything can be changed in the loop
3) pretested
4) the first initialization expression is evaluated just once but the other two expressions are evaluated with each iteration
2 key differences between C++and C count-controlled loops
In C++…
1) control expressions can be boolean
2) initial expression scope can include variable definitions and its scope is to the end of the function where defined
2 key differences between Java and C++ count-controlled loops
in Java…
1) control expressions must be boolean
2) the scope of variables defined in initial expression is only the loop body