KBP Chapter 8

1. A control structure is a control statement and the collection of statements whose execution it controls.

2. Böhm and Jacopini proved that all algorithms that can be expressed by flowcharts can be coded in a programming language with only two control statements: one for choosing between two control flow paths and one for logically controlled iterations

4. There is only one design issue that is relevant to all of the selection and iteration control statements: Should the control structure have multiple entries? All selection and iteration constructs control the execution of code segments, and the question is whether the execution of those code segments always begins with the first statement in the segment.

7. An F# selector have an else clause when the if expression does return a value

9. The design issues for multiple-selection statements:
• What is the form and type of the expression that controls the selection?
• How are the selectable segments specified?
• Is execution flow through the structure restricted to include just a single selectable segment?
• How are the case values specified?
• How should unrepresented selector expression values be handled, if at all?

14. The design issues for all iterative control statements:
• How is the iteration controlled?
• Where should the control mechanism appear in the loop statement?

15. The design issues for counter-controlled loop statements:
• What are the type and scope of the loop variable?
• Should it be legal for the loop variable or loop parameters to be changed in the loop, and if so, does the change affect loop control?
• Should the loop parameters be evaluated only once, or once for every iteration?

16. Pretest loop statements are set of statements to be executed repeatedly in which the test for loop completion occurs before the loop body is executed
Posttest loop statements are set of statements to be executed repeatedly in which the test for loop completion occurs after the loop body is executed

21. The design issues for logically controlled loop statements:
• Should the control be pretest or posttest?
• Should the logically controlled loop be a special form of a counting loop or a separate statement?

26. A user-defined iteration control is the one that issues a special call to the iterator, in which the iterator is called at the beginning of each iteration, and each time it is called, the iterator returns an element from a particular data structure in some specific order.

PROBLEM SET
1. Design issues should be considered for two-way selection statements:
• What is the form and type of the expression that controls the selection?
• How are the then and else clauses specified?
• How should the meaning of nested selectors be specified?
2. Python uses indentation to specify compound statements. Give an example in support of this statement.
if x > y :
x = y
print “case 1?
All statements equally indented are included in the compound statement.

5. What are the arguments pro and con, for Java’s approach to specify compound statements in control statements?
• Compound statements are required in control statements when the body of the if or else clause requires multiple statements.
• Java uses braces to form compound statements, which serve as the bodies of if and else clauses.

6. In C language, a control statement can be implemented using a nested if else, as well as by using a switch statement. Make a list of differences in the implementation of a nested if else and a switch statement. Also suggest under what circumstances the if else control structure is used and in which condition the switch case is used.
– Switch
• switch is usually more compact than lots of nested if else and therefore, more readable
• In many languages, switch only accepts only some data types.
if-else
• if allows complex expressions in the condition while switch wants a constant
• it accepts all data types
The if-else statement is used, in case if the programmer wants to the program to check whether a condition is true while the program is running, for example if the programmer wants to check whether a variable’s value is larger or smaller than x, he can use the if(var>x){stmts}.
The switch statements are usually used if the outcome of conditions are already known. For example ,in a menu input switch statement, the options are limited to either ‘Y’ or an ‘N’. Or numbered options. Using switch statement would allow the programmer to code with better readability than usign if-else statement.

14. State one of the main legitimate needs for gotos.
One of the main legitimate needs for gotos—premature exits from loops—can be met with highly restricted branch statements, such as break

Leave a comment