Concept of Programming Language Chapter 8

Assignment given by Mr Tri Djoko Wahjono

 

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

3. What is the definition of block ?
Block is a sequence of code delimited by either braces or the do and end reserved words.

4. What is/ are the design issue(s) for all selection and iteration control statements ?
Selection :
Two-way :
-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 ?
Multiple-Selection :
-On which type the selector is based ?
Iteration :
-How is the iteration controlled ?
-Where should the control mechanism appear in loop statement?

5. What are the design issues for selection structures ?
-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 ?

6. What is unusual about Python’s design of compound statements ?
Python uses indentation to specify compound statements. For example,
if x > y :
x = y
print “case 1″
equally indent statements are grouped as one compound statement.

7. Under what circumstances must an F# selector have an else clause ?
If the expression returns a value, it must have an else clause

9. What are the design issues for multiple-selection statements ?
-On which type the selector is based ?

14. What are the design issues for all iterative control statements ?
-How is the iteration controlled ?
-Where should the control mechanism appear in 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 Python do ?
It is used to count loops in Python

Problem Set
1. What 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.
Example :
if x > y :
x = y
print “case 1″

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.
It is useful for programmer who wants to check errors in their program. Rather than fully modifying their code, they can put some goto statement inside the if statement and return the value of the error in case if an error happens.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s