Friday, June 28, 2013

KBP - Chapter 8

Review Question

1. A control structure is a control statement and the collection of statements whose execution it controls.

2. They proved 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.

6. Python uses indentation to specify control statements and using a colon instead of then for a then clause.

12. It was based on multiple selection statement in ALGOL 68, which doesn’t have implicit branches from selectable segments.

19. In Python, range function does the most simple counting loops. The function takes 1, 2, or 3 variables. If range function has 1 variable (let’s say n), then it returns 0, 1, …, n. If range function has 2 variables (let’s say m and n), then it returns m, m+1, m+2, … , n-1. If range function has 3 variables, however (let’s say m,n,d) then it returns m, m+d, up before it goes to larger or equal to n.

25. What are the differences between the break statement of C++ and that of Java?


C++ has unconditional unlabeled exit with name break, while Java has unconditional labeled exit with the same name. C++ can only break the loop in which the break scope it was in, while Java can break straight to any targeted loop.

___________________________________________________
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 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

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. Example, to check whether variable a is equal to 1, 2, 3, or 4. Other example may be to check whether the choice of variable x is equal to ‘y’ or ‘n’. Switch(x){ case ‘y’: statement1; case ‘n’: statement2}

If-else, on the other hand, can be more practical, especially if the programmer wants to check a certain condition is true while the program is running. Example, checking whether variable var is smaller than 10. If (var<10){statement1} else {statement2}.

 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.

No comments:

Post a Comment