2013-06-23

Programming Language Concept (8)

Book : Concept of Programming Language

Page 380
Review Question
1. What is the definition of control structure?
A control structure is a control statement and the statements whose execution it controls

2. What did Böhm and Jocopini prove about flowcharts?
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

3. What is the definition of block?
In Ruby, a 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?
• 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?

5. What are the design issues for selection structures?
a) What is the form and type of the expression that controls the section?
b) Can a single statement, a sequence of statements, or a compound statement be selected?
c) 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

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

Page 382
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.




Tidak ada komentar:

Posting Komentar