Chapter 8 - Control Structures Flashcards

1
Q

What are control statements?

A

means of producing non-sequential execution of program statements through selection of alternative paths or repeating statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

definition of a control structure?

A

a control statement and the collection of statements whose execution it controls

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

definition of a selection statement

A

provides a means of choosing between two or more execution paths in a program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the 2 categories of selection statements?

A
2-way selection 
multiple selection (n-way)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

definition of compound statement

A

a group of statements controlled as a unit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The 4 design issues with selection

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Statement sequence vs a compound statement

A

statement sequence - a series of statements that are terminated with some reserved word

compound statement - see above. Surrounded by braces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The 4 PLs that have statement sequences in their then/else clauses instead compound statements

A

FORTRAN 95
Ada
Python
Ruby

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The main issue that arises with nested selectors?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

5 design issues with multiple selection statements

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

3 early multiple selector schemes in FORTRAN

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

4 key design choices of Pascal case statements

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

5 key design choices in C switch statements

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

definition of an iterative statement

A

a statement that causes another statement of collection of statements to be executed zero to many times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

2 main design issues with loops

A

1) how is iteration controlled

2) where is the control mechanism in the loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

2 main type of loops

A

1) count controlled loop- execute loop body a specified number of times
2) event controlled loop-execute loop body until some event occurs

17
Q

2 types of iteration control

A

pretest (before the loop body)

posttest (after the loop body)

18
Q

4 design issues with count-controlled loops

A

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

19
Q

4 count-controlled loop design choices in FORTRAN

A

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

20
Q

4 count-controlled loop design choices in Pascal

A

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

21
Q

4 count-controlled loop design choices in Ada

A

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

22
Q

4 count-controlled loop design choices in C (pre C99)

A

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

23
Q

2 key differences between C++and C count-controlled loops

A

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

24
Q

2 key differences between Java and C++ count-controlled loops

A

in Java…

1) control expressions must be boolean
2) the scope of variables defined in initial expression is only the loop body

25
Q

2 design issues with event controlled loops

A

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)

26
Q

1 design choice for event-controlled loop in Pascal

A

has separate pretest and posttest statements (can perform either)

27
Q

1 design choice for event-controlled loops in C/C++

A

has pretest and posttest loops but they are syntactically analogous

28
Q

1 design choice for event-controlled lops in Ada

A

has pretest BUT NO posttest

29
Q

1 design choice for event-controlled loops in FORTRAN

A

does not have either pretest or posttest loop but uses IF/GOTO instead

30
Q

What is a user-located loop control mechanism

A

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

31
Q

3 design issues with user-located loop control mechanisms

A

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

32
Q

User-located loop control mechanism in C, C++, Python, Ruby, and C#

A

an unconditional unlabeled exit (ie a ‘break’ statement)

33
Q

1 design choice for user-located loop control in Ada

A

has both conditional and unconditional exit from any number of levels (can exit any loop)

34
Q

What is meant by iteration based on data structures

A

using the order and number of elements of some data structure to control iteration

ie. think iterator while(p.next()) {…}

35
Q

definition of unconditional branching

A

a statement that transfers execution control to a specified location in the program

36
Q

3 PLs WITHOUT GoTo

A

Java
Python
Ruby

37
Q

7 PLs WITH GoTo

A
C
C++
FORTRAN
Pascal
Ada
C#
assembler
38
Q

1 main design issue with GoTo

A

what is the syntax for labels