cmps445 Lecture8
cmps445 Lecture8
LEVEL CONTROL
STRUCTURES
EXPRESSIONS
Expressions are the fundamental means of specifying
computations in a programming language
Arithmetic Expressions
Unary e.g. -5
Binary e.g. 2 * 3
Ternary e.g. x ? y : z
Precedence levels (BEDMAS)
OPERATORS AND TYPES
Overloaded operators
use of an operator for more than one purpose
Type conversions
Narrowing conversion e.g. float to int
Widening conversion e.g. int to float
Implicit (coercion ) vs explicit (cast) type conversion
MORE EXPRESSIONS
Relational and Boolean expressions
Short circuit evaluation
Assignment statements
Multiple assignments
Mixed-mode assignments
TOPICS
Introduction
Selection Statements
Iterative Statements
Unconditional Branching
Guarded Commands
Conclusions
INTRODUCTION
▪ Levels of Control Flow
▪ Within expressions (Chapter 7)
▪ Among program units (Chapter 9)
▪ Among program statements (this chapter)
INTRODUCTION
Def: Statements that provide capabilities such as selecting
among alternative control flow paths or causing the repeated
execution of certain collection of statements are called control
statements
Def: A control structure is a control statement and the
statements whose execution it controls
Control
Structures
One important result: It was proven that all flowcharts can be coded with
only two-way selection and pretest logical loops
Language features that help make control statement design easier is a
method of forming statement collections.
Compound statements introduced by ALGOL60 in the form of begin.. .end
A block is a compound statement that can define a new scope (with local variables)
SELECTION STATEMENTS
A selection statement provides the means of choosing between
two or more paths of execution
Two general categories:
Two-way selectors
Select one of two execution paths: if-then-else statements.
Multiple-way selectors
Select one or multiple out of multiple execution paths: if-elseif and
switch statements.
TWO-WAY SELECTION STATEMENTS
The general form is:
if control_expr
then clause
else clause
Design issues
What is the form and type of the control expression?
How are the then and else clauses specified?
How should the meaning of nested selectors be specified?
TWO-WAY SELECTION STATEMENTS
The Control Expression:
Specified in parenthesis when the then reserved word is not used (as in
C, C++, C#).
Either arithmetic or Boolean expressions can be used (such as C++ and
Python).
In other languages only Boolean expressions can be used for control
expression (such as Ada, Java and C#).
TWO-WAY SELECTION STATEMENTS
Clause Form
ALGOL 60 if: Control expression
else statement
The statements could be single or compound
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
C-based languages use braces to form compound statements
In Fortran 95, Ada, Python, and Ruby, clauses are statement sequences
Python uses indentation to define clauses
if x > y :
x = y
print " x was greater than y"
TWO-WAY SELECTION STATEMENTS
Nested Selectors
e.g. (Java) if ...
if ...
...
else ...
Which if gets the else?
Java's static semantics rule: else goes with the nearest if
NESTING SELECTORS
To force an alternative semantics, compound statements may be
used:
if (sum == 0) {
if (count == 0)
result = 0;
}
else result = 1;
Advantage: readability
NESTING SELECTORS
In Ruby: In Python
if sum == 0 then if sum == 0 :
if count == 0 then if count == 0 :
result = 0 result = 0
else
else :
result = 1
result = 1
end
end
SELECTOR EXPRESSIONS
In ML, F#, and LISP, the selector is an expression
F#
let y =
if x > 0 then x
else 2 * x;;
switch (value) {
case -1:
Negatives++;
break;
case 0:
Positives++;
goto case 1;
case 1:
Positives ++;
break;
default:
Console.WriteLine(“Error in switch \n”);
}
MULTIPLE-WAY SELECTION: EXAMPLES
Ruby
leap = case
when year % 400 == 0 then true
when year % 100 == 0 then false
else year % 4 == 0
end
posttest
LOGICALLY-CONTROLLED LOOPS
Language Examples:
1. C and C++ have both pretest and posttest, but the
control expression for the posttest version is treated just
like in the pretest case (while and do - while)
2. Java is like C, except the control expression must be
Boolean and the body can only be entered at the
beginning--Java has no goto
LOGICALLY-CONTROLLED LOOPS
Language Examples:
3. Ada has a pretest version, but no posttest
4. FORTRAN 77 and 90 have neither
5. Perl has two pretest logical loops, while and until, but no
posttest logical loop
USER-LOCATED LOOP CONTROL MECHANISMS
Design issues:
1. Should the conditional be part of the exit?
2. Should control be transferable out of more than one
loop?
USER-LOCATED LOOP CONTROL MECHANISMS
Language Examples:
C , C++, C# and Java – break
Perl - last
In Java and Perl, you can use a labelled exit (specify which loop
to exit)
Not available in C, C++, python, Ruby and C#
Problem: readability
Some languages do not have them: e.g., Java
GUARDED COMMANDS
GUARDED COMMANDS
Dijkstra, 1975
Basis for two linguistic mechanisms for concurrent programming
(in CSP, Ada, Haskell)
Basic Idea: if the order of evaluation is not important, the
program should not specify one
Purpose: to support a new programming methodology
(verification during program development)
GUARDED COMMANDS
Selection: if <boolean> -> <statement>
[]<boolean> -> <statement>
...
[] <boolean> -> <statement>
fi
Semantics: when this construct is reached,
Evaluate all Boolean expressions
If more than one are true, choose one nondeterministically
If none are true, it is a runtime error
GUARDED COMMANDS
EXAMPLE
To find the largest of two numbers, we can use
if x >= y -> max := x
[] y >= x -> max := y
fi
This computes the desired result without over specifying
the solution.
If x and y are equal, it does not matter which we assign to
max.
GUARDED COMMANDS
Loops
do <boolean> -> <statement>
[] <boolean> -> <statement>
...
[] <boolean> -> <statement>
od
GUARDED COMMANDS
Semantics: For each iteration:
Evaluate all boolean expressions
If more than one are true, choose one nondeterministically; then start
loop again
If none are true, exit loop
GUARDED COMMANDS
EXAMPLE
Consider the following problem: Given four integer variables,
q1, q2, q3,and q4, we need to rearrange the values of the four
so that q1 ≤ q2 ≤ q3 ≤ q4.
Solution using guarded commands:
do q1 > q2 -> temp := q1; q1 := q2; q2 := temp;
[] q2 > q3 -> temp := q2; q2 := q3; q3 := temp;
[] q3 > q4 -> temp := q3; q3 := q4; q4 := temp;
od
REFERENCES
58
SUBPROGRAMS