Chapter 8
What is multiple-way selection using if?
used in else-if clauses. if(){ } else if(){ } else if(){ }
What are design choices for C switch statements
1. Control expression can be only an integer type 2. Selectable segments can be statement sequences, blocks, or compound statements 3. Any number of segments can be executed in one execution of the construct 4. default clause is for unrepresented values (no default whole thing does nothing)
Counter controlled loops in c vs c++
1. The control expression can also be Boolean 2. The initial expression can include variable definitions.
What are four design choices for counter controlled loops?
1. There is no explicit loop variable 2. Everything can be changed in the loop 3. The first expression is evaluated once, but the other two are evaluated with each iteration 4. It is legal to branch into the body of a for loop in C
What are some PHP iteration clauses based on data structures
1. current - points at one element of the array 2. next -moves current to the next element 3. reset - moves current to the first element
Design issues for nested loops? (1)Should conditional be part of the exit and (2) should control be transferable out of more than one loop?
1. yes, it needs to be apart of each other so there is an exit and so it is not an infinite loop 2. yes it can be otherwise it would be better to have one loop.
What are multiple way selection statements
Allow the selection of one of any number of statements or statement groups. Example: switch statements. switch(expr){ case const_expr: stmt1; case const_expr2: stmt2; default: stmt;
How is iteration controlled?
By using multiple control statements. for(int i = 0; i < 5; i++);
What are two way selection statements and design issues? What is the form and type of control expression? How are then and else clauses specified? - How should meaning of nested selectors be specified?
Form: if(control expression) then clause else clause. (1) boolean. (2)After a control expression, true = then false = else. (3) Based on higher priority in start of if/else and else ifs.
What is unconditional branching and what is its main concern
It transfers execution control to a specified place in the program. main concern is readability.
What are nesting selectors
Java example: if(sum == 0) if(count == 0) result = 0; else result = 1; The else matches with the nearest if
For counter controlled loops, What is the value of the loop variable after loop termination?
Null
For counter controlled loops, should the loop parameters be evaluated only once, or once for every iteration?
Once for every iteration otherwise the loop may not end when it is supposed to which can lead to more problems.
Iteration based on data structures and the control mechanism for them
The control mechanism is a call to an iterator function that returns the next element in some chosen order, if there is one; else loop is terminate.
For counter controlled loops, What are the type and scope of the loop variables?
The type is integers and the scope is just within the loop unless declared outside the loop.
Rationale for guarded commands
Verification is possible with only selection and logical pretest loops 2. Verification is relatively simple with only guarded commands
Loop guarded commands:
do < boolean> -> stmt ... For each iteration: evaluate all boolean expressions. If more than one true, choose one then start loop again. If none true, exit loop
Selection guarded command:
if <boolean expression> -> stmt ... Evaluates all boolean expressions. if more than one true it chooses one. If none true, runtime error
Python nesting selectors
if sum == 0 : if count == 0 : result =0 else : result = 1
Nesting selectors using { }
if(sum == 0){ if(count == 0) result = 0; } else result = 1;
Where is the control mechanism in the loop?
strong precondition and also control statements of the loop.