Pages

Sunday 14 April 2013

Chapter 8

An assignment from Mr. Tri Djoko Wahyono (again). As what the title said, it is the answer for some question (8 numbers, quite randomly in Review Qustion and 3 or 4 numbers in Problem Set) in Chapter 8 in "Concept of Programming Language" book.


Review Question

1. What is the definition of control structure?
Control structure is a control statement and the collection of the statements whose execution it controls.

6. What is unusual about Phyton's design of compound statements?
Phyton uses indentation to specify compound statements.

7. Under what circumtances must an F# selector have an else clause?
If the "if" expression does return a value, it must have an else clause.

9. What are 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 segement?
  • How are the case of values specified?
  • How should unrepresented selector expression values be handled, if at all?

14. What are the design issues for all iterative control statements?

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

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

19. What does the range function in Phyton do?
range function is used for most simple counting loops in Phyton.

22. What is the main reason user-located loop control statements were invented?
In some situation, it is convenient for a programmer to choose a location for loop control other than the top bottom of the loop body.

Problem Set
2. Phyton uses indentation to specify compound statements. Give an example in support of this statement.
If x < y :
  x = y
  print "This is in compound statement"

6. In C language, a control statement can be implemented using a nested if else, as well as by using 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 id else control stricture is used and in which condition the switch case is used.

If else structure
  • Used when the variable that checked have some range, like 0-50, 51-64, A-E, etc.
  • Used when the logic expression to compare the variable is many.
  • When there's only if statement without else, when the variable isn't valid at the logic expression there, the compound statements is skipped.
Switch case structure
  • Used when the variable that checked is exact value, like 1, 40, A, C, etc.
  • Used when the logic expression is just one but have a different output value each option.
  • When there's switch statement without default, when the variable isn't valid at any values there, the compound statements is skipped.


18. State one of the main legitimate needs for gotos.
Control flow transfers out of a loop where there's a large amount of lexical context that the loop required.

No comments:

Post a Comment