nav

       HOME    BINUS EVENT   HOW TO   KBP   THOUGHTS

About



Friday, January 2, 2015

Assignment #8

Source:
Concepts of Programming Languages, Robert W. Sebesta.
Chapter 8: Statement-Level Control Structures, page 381-382

By:
Name : Helena Natanael
NIM  : 1801380333
P.S.: Happy New Year 2015! :)


Review Question:

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 "value of x is modified".
All statements equally indented are included in the compound statement.



7. Under what circumstances must an F# selector have an else clause?
An F# selector have an “else” clause if the “if” expression does return a value.


8.What are the common solutions to the nesting problem for two-way selectors?
A programmer should avoid syntactic entity because in some cases, syntactic entity leads to ambiguous meaning when it is compiled. Another way to avoid the issue of nested selection statements is to use an alternative means of forming compound statements.


 9. What are the design issues for multiple-selection statements?
1. What is the form and type of the expression that controls the selection?• How are the selectable segments specified?
2. Is execution flow through the structure restricted to include just a single selectable segment?
3. How are the case values specified?
4. How should unrepresented selector expression values be handled, if at all?


10. Between what two language characteristics is a trade-off made when deciding whether more than one selectable segment is executed in one execution of a multiple selection statement?
The C# switch statement differs from that of its C-based predecessors in two ways. First, C# has a static semantics rule that disallows the implicit execution of more than one segment. The rule is that every selectable segment must end with an explicit unconditional branch statement: either a break,
which transfers control out of the switch statement, or a goto, which can transfer control to one of the selectable segments (or virtually anywhere else).

As with C, if there is no break at the end of the selected segment, execution continues into the next segment.




Problem Sets:

6. Analyze the potential readability problems with using closure reserved words for control statements that are the reverse of the corresponding initial reserved words, such as the case-esac reserved words of ALGOL 68. For example, consider common typing errors such as transposing characters.
The potential readability problem is the typing errors. It’s very possible to occur if we don’t type the code carefully.


7. Use the Science Citation Index to find an article that refers to Knuth (1974). Read the article and Knuth’s paper and write a paper that summarizes both sides of the goto issue.
An alternative viewpoint is presented in Donald Knuth's Structured Programming with go to Statements, which analyzes many common programming tasks and finds that in some of them GOTO is the optimal language construct to use.[7] In their quasi-standard book on the C programming language, Dennis Ritchie and Brian Kernighan warn that goto is "infinitely abusable", but also suggest that it could be used for end-of-function error handlers and for multi-level breaks from loops.


8. In his paper on the goto issue, Knuth (1974) suggests a loop control statement that allows multiple exits. Read the paper and write an operational semantics description of the statement.
Operational semantics are a category of formal programming language semantics in which certain desired properties of a program, such as correctness, safety or security, are verified by constructing proofs from logical statements about its execution and procedures, rather than by attaching mathematical meanings to its terms (denotational semantics).


9. What are the arguments both for and against the exclusive use of Boolean expressions in the control statements in Java (as opposed to also allowing arithmetic expressions, as in C++)?
The primary argument for using Boolean expressions exclusively as control expressions is the reliability that results from disallowing a wide range of types for this use. In C, for example, an expression of any type can appear as a control expression, so typing errors that result in references to variables of incorrect types are not detected by the compiler as errors. No , it would not be a good idea. Although this custom precedence sounds like increasing flexibility, requiring parentheses to show a custom precedence would impact in readability and writability of a program.


10. In Ada, the choice lists of the case statement must be exhaustive, so that there can be no unrepresented values in the control expression. In C++, unrepresented values can be caught at run time with the default selector. If there is no default, an unrepresented value causes the whole statement to be skipped. What are the pros and cons of these two designs (Ada and C++)? 

---- C++ :
1. Control expression can be only an integer type 
2. Selectable segments can be statement sequences, blocks, or compound statements 
3. Construct is encapsulated 
4. Any number of segments can be executed in one execution of the construct (there is no implicit branch at the end of selectable segments) (a trade-off between reliability and flexibility–convenience) - To avoid it, the programmer must supply a break statement for each segment.
5. default clause is for unrepresented values (if there is no default, the whole statement does nothing) 
 ----- Ada:
 1. Constant lists can include: - Subranges e.g., 10..15
- Boolean OR operators
e.g., 1..5 | 7 | 15..20
2. Lists of constants must be exhaustive - Often accomplished with others clause
- This makes it more reliable.

No comments:

Post a Comment