Chapter 8 Not
Chapter 8 Not
Control Structure
A control structure is a control statement and the statements whose
execution it controls.
Design Question
Should a control structure have multiple entries?
Selection Statements
A selection statement allows choosing between two or more execution paths.
Two-way selectors
Multiple-way selectors
Design Issues:
Page 1
Created by Turbolearn AI
Clause Form
In many contemporary languages, the then and else clauses can be single
statements or compound statements.
In Perl, all clauses must be delimited by braces (they must be compound).
In Fortran 95, Ada, Python, and Ruby, clauses are statement sequences.
Python uses indentation to define clauses.
Nesting Selectors
Java example:
if (sum == 0)
if (count == 0)
result = 0;
else
result = 1;
Page 2
Created by Turbolearn AI
if (sum == 0) {
if (count == 0)
result = 0;
} else
result = 1;
if sum == 0 then
if count == 0 then
result = 0
else
result = 1
end
end
Python
if sum == 0 :
if count == 0 :
result = 0
else :
result = 1
Selector Expressions
In ML, F#, and LISP, the selector is an expression
Page 3
Created by Turbolearn AI
Design Issues:
1. What is the form and type of the control expression?
2. How are the selectable segments specified?
3. Is execution flow through the structure restricted to include just a single
selectable segment?
4. How are case values specified?
5. What is done about unrepresented expression values?
Examples
C, C++, Java, and JavaScript:
switch (expression) {
case const_expr1:
stmt1;
...
case const_exprn:
stmtn;
[default: stmtn+1]
}
Page 4
Created by Turbolearn AI
C#
Each selectable segment must end with an unconditional branch (goto or break).
The control expression and the case constants can be strings.
Ruby
leap = case
when year % 400 == 0 then true
when year % 100 == 0 then false
else year % 4 == 0
end
if count < 10 :
bag1 = True
elif count < 100 :
bag2 = True
elif count < 1000 :
bag3 = True
case
when count < 10 then bag1 = true
when count < 100 then bag2 = true
when count < 1000 then bag3 = true
end
Page 5
Created by Turbolearn AI
Iterative Statements
The repeated execution of a statement or compound statement is accomplished
either by iteration or recursion.
Counter-Controlled Loops
A counting iterative statement has a loop variable and a means of specifying the
initial, terminal, and stepsize values.
Design Issues:
1. What are the type and scope of the loop variable?
2. Should it be legal for the loop variable or loop parameters to be changed in the
loop body, and if so, does the change affect loop control?
3. Should the loop parameters be evaluated only once, or once for every iteration?
Examples
Ada
Page 6
Created by Turbolearn AI
Design choices:
Type of the loop variable is that of the discrete range (A discrete range is a sub-
range of an integer or enumeration type).
Loop variable does not exist outside the loop.
The loop variable cannot be changed in the loop, but the discrete range can; it
does not affect loop control.
The discrete range is evaluated just once.
Cannot branch into the loop body.
C-based languages
Design choices:
Python
Page 7
Created by Turbolearn AI
The object is often a range, which is either a list of values in brackets ([2, 4,
6]), or a call to the range function (range(5), which returns 0, 1, 2, 3, 4).
The loop variable takes on the values specified in the given range, one for each
iteration.
The else clause, which is optional, is executed if the loop terminates normally.
F#
Because counters require variables, and functional languages do not have variables,
counter-controlled loops must be simulated with recursive functions
This defines the recursive function forLoop with the parameters loopBody (a function
that defines the loop’s body) and the number of repetitions. () means do nothing and
return nothing.
while(i<5):
if(i==3)
break;
Page 8
Created by Turbolearn AI
Have an unlabeled control statement, continue, that skips the remainder of the
current iteration but does not exit the loop.
PHP
Page 9
Created by Turbolearn AI
Ruby
Blocks are sequences of code, delimited by either braces or do and end. Blocks can be
used with methods to create iterators. Predefined iterator methods (times, each, upto):
Ruby has a for statement, but Ruby converts them to upto method calls.
Page 10
Created by Turbolearn AI
Unconditional Branching
Transfers execution control to a specified place in the program.
Represented one of the most heated debates in the 1960s and 1970s.
Major concern: Readability.
Some languages do not support goto statement (e.g., Java).
C# offers goto statement (can be used in switch statements).
Loop exit statements are restricted and somewhat camouflaged goto's.
Conclusions
Variety of statement-level structures. Choice of control statements beyond selection
and logical pretest loops is a tradeoff between language size and writability.
Functional and logic programming languages use quite different control structures.
Page 11