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
2 design issues with event controlled loops
1) should it be pretest or posttest
2) should this be a special case of the count-controlled loop or should it be its own statement (presumably with its own unique/independent syntax)
1 design choice for event-controlled loop in Pascal
has separate pretest and posttest statements (can perform either)
1 design choice for event-controlled loops in C/C++
has pretest and posttest loops but they are syntactically analogous
1 design choice for event-controlled lops in Ada
has pretest BUT NO posttest
1 design choice for event-controlled loops in FORTRAN
does not have either pretest or posttest loop but uses IF/GOTO instead
What is a user-located loop control mechanism
a location for loop control chosen by the programmer that is at neither the top nor the bottom of the loop body
They use user-located /defined loop exits
3 design issues with user-located loop control mechanisms
1) should the conditional (the original loop condition) be part of the exit
2) should the mechanism be allowed in an already controlled loop (as opposed to an infinite loop)
3) should control be transferable out of more than one loop
User-located loop control mechanism in C, C++, Python, Ruby, and C#
an unconditional unlabeled exit (ie a ‘break’ statement)
1 design choice for user-located loop control in Ada
has both conditional and unconditional exit from any number of levels (can exit any loop)
What is meant by iteration based on data structures
using the order and number of elements of some data structure to control iteration
ie. think iterator while(p.next()) {…}
definition of unconditional branching
a statement that transfers execution control to a specified location in the program
3 PLs WITHOUT GoTo
Java
Python
Ruby
7 PLs WITH GoTo
C C++ FORTRAN Pascal Ada C# assembler
1 main design issue with GoTo
what is the syntax for labels