Chapter 8-Concepts of Programming Languages(Robert W. Sebesta)-Mr. Tri Djoko Wahjono, Ir, M.Sc.

Review Question

1. What is the definition of control structure?

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

 

4. What is/are the design issue(s) for all selection and iteration control statements?

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.

 

5. What are the design issues for selection structures?           

The design issues for selection structures are:

– 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?

 

9. What are the design issues for multiple-selection statements?

The design issues for multiple-selection statements are:

– 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. What are the design issues for all iterative statements?

The design issues for all iterative statements are:

– How is the iteration controlled?

– Where should the control mechanism appear in the loop statement?

 

15. What are the design issues for counter-controlled loop statements?

The design issues for counter-controlled loop statements are:

– 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. What is a pretest loop statement? What is a posttest loop statement?

Pretest is the test for loop completion occurs before the loop body is executed and posttest means that it occurs after the loop body is executed.

 

19. What does a range function in Python do?

Range function is to determine the range of the looping, the initial point and the end point of the looping. For example:

range(5) returns [0, 1, 2, 3, 4]

range(2, 7) returns [2, 3, 4, 5, 6]

range(0, 8, 2) returns [0, 2, 4, 6]

 

20. What contemporary languages do not include a goto?

Java language is the contemporary language that doesn’t include a goto, the loop bodies cannot be entered anywhere but at their beginning.

 

21. What are the design issues for logically controlled loop statements?

The design issues for logically controlled loop statements are:

– Should the control be pretest or posttest?

– Should the logically controlled loop be a special form of a counting loop or a separate statement?

 

23. What are the design issues for user located loop controlled mechanism?

The design issues for user located loop controlled mechanism are:

– Should the conditional mechanism be an integral part of the exit?

– Should only one loop body be exited, or can enclosing loops also be exited?

 

29. How are iterators implemented in Ruby?

Ruby predefines several iterator methods, such as times and up to for counter-controlled loops, and each for simple iterations of arrays and hashes.

 

Problem set

 

1. What design issues should be considered for two-way selection statements?

-What the form and type of the expression that controls the selection is.

-How the then and else clauses are specified.

– How the meaning of nested selectors should be specified.

  

2. Python uses indentation to specify compound statements. Give an example in support of this statement.

def perm(l):

# Compute the list of all permutations of l

if len(l) <= 1:

return [l]

r = []

for i in range(len(l)):

s = l[:i] + l[i+1:]

p = perm(s)

for x in p:

r.append(l[i:i+1] + x)

return r

 

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
  • If you omit the break between two switch cases, you can fall through to the next case in many C-like languages. With if else you’d need a goto (which is not very nice to your readers … if the language supports goto at all).
  • In most languages, switch only accepts primitive types as key and constants as cases. This means it can be optimized by the compiler using a jump table which is very fast.
  • It is not really clear how to format switch correctly. Semantically, the cases are jump targets (like labels for goto) which should be flush left. Things get worse when you have curly braces:
  • case XXX: {
  • } break;

Or should the braces go into lines of their own? Should the closing brace go behind the break? How unreadable would that be? etc.

  • In many languages, switch only accepts only some data types.

if-else

  • if allows complex expressions in the condition while switch wants a constant
  • You can’t accidentally forget the break between ifs but you can forget the else(especially during cut’n’paste)
  • it accepts all data types.

Suggestion:

Generally both of them have the same concept but when you have more than one option including some condition, it is suggested to use if-else rather than switch. Because switch only accepts primitive types as key and constants as cases.