Statement-Level Control Structures
Written on 17.45 by Unknown
We meet yet again through my blog and this time I'm gonna talk about my eight assignment from Mr. Tri Djoko Wahjono, Ir, M.Sc. It is about "Statement-Level Control Structures", the eighth chapter from Sebesta's Programming Language Concepts book, so i answered 5 questions from the review questions and 5 questions from the problem sets. So here is my answer for them :
Review Questions :
1. What is the definition of control structure?
- A control structure is a control statement and the collection of statements whose execution it controls.
2. What did Böhm and Jocopini prove about flowcharts?
- They had proven that all algorithms that can be expressed by flowcharts can be coded in a programming languages with only two control statements: one for choosing between two control flow paths and one for logically controlled iterations.
- 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?
- 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 ?
Problem Sets :
1. Describe three situations where a combined counting and logical looping statement is needed.
- Three situations in which a combined counting and logical control loops are:
- A list of values is to be added to a SUM, but the loop is to be exited if SUM exceeds some prescribed value.
- A list of values is to be read into an array, where the reading is to terminate when either a prescribed number of values have been read or some special value is found in the list.
- The values stored in a linked list are to be moved to an array, where values are to be moved until the end of the linked list is found or the array is filled, whichever comes first.
2. Study the iterator feature of CLU in Liskov et al. (1981) and determine its advantages and disadvantages.
- The key addition was the concept of a cluster, CLU’s type extension system and the root of the language’s name CLUster. Clusters correspond generally to the concept of an “object” in an OO language, and have roughly the same syntax.
- CLU did not offer any sort of structure for the clusters themselves. Cluster names are global, and no namespace mechanism was provided to group clusters or allow them to be created “locally” inside other clusters. This problem is not unique to CLU, but it is surprising that so many languages have lacked this feature — given the centralness in ALGOL of giving scope to variables, it seems that giving scope to cluster/object names would be an obvious extension.
- CLU does not perform implicit type conversions. In a cluster, the explicit type conversions ‘up’ and ‘down’ change between the abstract type and the representation. There is a universal type ‘any’, and a procedure force[] to check that an object is a certain type.
- Another key feature of the CLU type system are iterators, which return objects from a collection one after the other. Iterators were “black boxes” that offered an identical API no matter what data they were being used with. Thus the iterator for a collection of complex_numbers would be identical to that for an array of integers.
- CLU also includes exception handling, based on various attempts in other languages; exceptions are raised using signal and handled with except. Oddly, given the focus on type design, CLU does not offer enumerated types, nor any obvious way to create them.
- A final distinctive feature in CLU is multiple assignment, where more than one variable can appear on the left hand side of an assignment operator
- C# because the C# multiple selection structures is a great boost to C# writability, with no obvious negatives, furthermore C# control statement is the most flexible iteration statement.
4. What are the pros and cons of using unique closing reserved words on compound statements?
- Pros: Readability. When one sees an end-if or end-while in a program written by someone else, it is clear which block is ending.
- Cons: Writability. The use of these reserved words means that there are more reserved words. More reserved words means less words available to the programmer. It also means more words to learn.
5. What are the arguments, pro and con, for Python’s use of indentation to specify compound statements in control statements?
Pros of indentation:
- Helps reduce inconsistent indentation in code which makes it easier to read (in other words consistency)
- clears the screen by replace visible tokens with whitespace to serve the same purpose
Cons of indentation:
- Much easier to cut and paste code to different levels (you don’t have to fix the indentation)
- More consistent. Some text editors display whitespace(s) differently.
- You cannot safely mix tabs and spaces in Python such that it would be easy to cause an error by putting too few spaces in an indentation level, thus going to the previous indentation level and closing the loop/block. This decreases writability.