Docsity

CS 403 Control Flow: Sequencing, Selection, Iteration, Variable Init., Exams of Programming Languages

Partial preview of the text

Download CS 403 Control Flow: Sequencing, Selection, Iteration, Variable Init. and more Exams Programming Languages in PDF only on Docsity!

1

CS 403 - Programming

Languages

Lecture 16

October 16, 2001

Today’s Agenda

Announcement: Exam 2 is postponed

until Tuesday, November 6 (by one

week)

Programming help file is now on the

Web

New topic: Chapter 6 (control flow)

Control Flow Mechanisms

(Chap. 6)

  • Sequencing
  • Selection
  • Iteration
  • Procedural abstraction
  • Recursion
  • Concurrency
  • Nondeterminacy

Sequencing

Executing series of statements in the order

they appear in the program text

Obviously a trivial idea

Assignment and I/O statements are executed

in text order:

cin >> x;

x = x + 3;

y = x + 5;

cout << y << endl;

Costs and Issues

Strategy 1: Ignore uninitialized

variables:

 Doesn’t cost anything

 Can result in really strange behavior

 A function with an uninitialized variable can

exhibit different behavior on different calls.

Example

void foo(void){
int x;
cout << x << endl;
void main(void){
ugh();
foo();
blah();
foo();

Costs and Issues (cont.)

Strategy 2: Initialize all variables to

default

 Static variables can be initialized at compile

time

 This is done in C++, for example.

 Stack and heap variables incur run-time

initialization cost

Costs and Issues (cont.)

Strategy 3: Give an error when an

uninitialized variable is used.

Issue: A use of an uninitialized variable

is typically preceded by more than one

execution path.

Implementing Strategy 3

(generating error messages)

Compiler can generate an error messages:

 When every execution path preceding the use of a variable fails to contain a definition (this is ok, but it’s seldom this simple)  When some execution path preceding the use of a variable fails to contain a definition  Java does this  It’s quite restrictive – may give an error when no problem will actually occur in practice

Compromise solution:

 Generate errors in the easy (every path) case  Generate warnings in the second case  (Optionally) Employ run-time detection of variable usage errors  This is extremely expensive and seldom practiced for compiled languages  Typically requires using an extra-bit flag

Basic Concepts

Variables: Basic way to move data

around

Variable initialization

Expression evaluation:

 Precedence orderings

 Short-circuiting

Expression Evaluation

Two issues:

 Precedence and associativity

 Order of evaluation

These are not quite the same issue,

although they seem to be closely

related

Expression Evaluation

Examples:

 a + b * c – 3
 a < b && c < d
 a++ * c – 3

Different languages have slightly different

precedence rules

 And often different operators anyway…
 Pascal vs. C on a < b && c < d
 Use parentheses when in doubt

Efficiency Improvement:

Example

Suppose you have:

a = b + c; d = c + e + b;

“b+c” is a common subexpression
So compiler could generate code equivalent to:

a = b + c; d = a + e; Moral: Programmer not typically allowed to make any assumption about the order of subexpression computation in c

  • e + b.  Compiler may order the execution of an expression to maximize efficiency  Terms of expressions should therefore NOT induce side effects (as on the previous slide)

Exception to this Rule: Short

Circuiting

Guarantees a particular order of execution

 Done for efficiency reasons

Basic idea:

 a < b && c < d: First part false, don’t evaluate
second
 a < b || c < d: First part true, don’t evaluate
second

C++ uses short circuiting for these operators

Short Circuiting Aids

Programming

Linked list example:

p = my_list;

while (p && p->key != val)

p = p->next;

First part false makes the second part invalid.

 Without short circuiting, this causes an error

Next Time

• Look at other control flow

mechanisms, principally:

• Selection

• Iteration

• Procedural abstraction

• Recursion