Concept of Programming Languages by Robert W. Sebesta Answer
Review Questions
1. What is the definition of control structure ?
Linguistic mechanisms that selecting among alternative control flow paths and causing repeated execution of statements or sequence statements.
3. What is the definition of block ?
Block is a sequence of code delimited by either braces or the do and end reserved words.
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 control statements?
Python uses indentation to specify control statements and using a colon instead of then for a then clause.
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 ?
12. On what previous language was C’s switch statement based?
It was based on multiple selection statement in ALGOL 68, which doesn’t have implicit branches from selectable segments.
16. What is pretest loop statement? What is a posttest loop statement?
Pretest loop statement is when the test for loop completion occurs before the loop body is executed, while posttest loop statement is when the test for loop completion occurs after the loop body is executed.
12. On what previous language was C’s switch statement based?
Operator that has different implementation depending on its arguments.
17. What is the difference between the for statement of C++ and that of Java?
The loop control expression in Java is restricted to boolean, unlike that of C++, even though the syntax is similar.
19. What does the range function in Python do ?
It is used to count elements of loops in Python
Problem Set
1. What design issues should be considered for two-way selection statements?
The design issues are:
- What is the form and type of the expression that controls the selection?
- How are 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 true:
x=y
print “indentation works!”
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
More compact than lots of nested if else, therefore it has more readability.
Not quite flexible, as in some languages it can only available to certain (even sometimes should be similar) data types.
If-else
Allows more complex expressions and various possible data types as conditions.
Quite hard to read when it’s too nested.
Switch statement is used to check mostly when a certain variable is equal to a certain value.
11. Explain the advantages and disadvantages of the Java switch statement, compared to C++’s switch statement.
Java’s variable in the argument of a switch statement can be of integeral type (byte, short, int, etc), char, and String (JDK 1.7 and newer versions), but C++ can only be int or char.
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.
0 comments:
Post a Comment