CS 206 – C 3 – Flow of Control

Sequential Execution vs Execution Flow Control…

3 .1 Relational, Equality and Logical Operators for Flow Control

(Operators which produce True/False Results…)

! operator is unary, i.e., single operand.

All of these other operators are binary and produce int values of:

1 (Any nonzero value represents True in C), or

0 (Any zero value represents False in C).

3.2 Relational Operators/Expressions (< > <= >=)

3.3 Equality Operators and Expressions

3.4 Logical Operators and Expressions

And/Or (Bitwise Operations)

‘Short Circuit Evaluation…”

Evaluate the expression only until result is known, i.e., Or ops.

3.5 Compound Statement

a=1;

b=2;

c=3;

3.6 Empty Statement

A=b:

b=c;

Useful when a statement is required syntactically but not algorithmically.

Ex:

If (a==b)

3.7 if / if-else Statements

Sample Program – Min of Three Numbers

3.8 The while Statement – a looping mechanism

While ( expr )

statement

next statement

Execution :

1. expr is evaluated

2. If expr is true (nonzero), then statement is executed and execution continues at the beginning of the while loop.

3. If expr is false (zero), then the loop is terminated by executing next statement .

Example…

3.9 Problem – Finding Max Values

3.10 The for Statement

for (expr1; expr2; expr3)

statement

next statement

executionally equivalent to:

expr1;

while (expr2) {

statement

expr3;

next statement

Execution sequence:

. Evaluate expr1 ( L oop initiation…)

. LoopStart - Evaluate expr2 . If expr2 is False (Z), exit loop to next statement .

If expr2 is True (NZ), execute statement , evaluate expr3 . GoTo

LoopStart.

Example: for (i = 1; i <= n; ++i)

factorial *= i;

Notes:

. If expr1 is missing, no loop initiation is performed. Should be done prior to LoopStart.

. If expr2 is missing, the non-expr2 test is always True , therefore exit loop via if/else, goto, etc. from within the loop.

. If expr3 is missing, a) control parameter in expr2 has to be changed inside the loop, or b) exit loop via if/else, goto, etc. from within the loop.

3.11 Boolean Variable Problem

Create a table of values for: b1 || b3 || b5 and b1 && b2 || b4 && b5

3.12 The Comma Operator

Syntax: expr1 , expr2

expr1 is evaluated first, then expr2. Comma expression has type/value of its rightmost expression.

Examples:

a = 0, b = 1

++i, sum += i

for (sum = 0, i = 1; i <= n ; sum += i, ++i);

3.13 The do Statement (Do-While Loop )

Syntax:

do

statement

while (expr);

next statement

Execution:

. statement is executed. expr is evaluated. If expr is True (NZ), transfer to beginning of loop. If expr is False (Z), transfer to next statement.

Example:

3.14 The go to Statement

Unconditional execution transfer to a labeled statement somewhere within the current function.

Syntax:

goto label;

label: statement

Examples:

3.15 break and continue Statements

Syntax: break; continue;

b reak statement causes an exit from the innermost enclosing loop (or switch statement).

Example:

continue statement causes termination of the current iteration of a loop (for, while and do loops only) and immediate execution of the next iteration.

Examples:

3.16 switch Statement

switch statement is equivalent to a multiple if-else /goto statement sequence.

Syntax:

switch (expr) {

case 1:

break;

case 3:

break;

case 5:

case 10:

default:

Notes:

. expr must evaluate to an int

. case labels must be unique

. break statement causes transfer out of switch body compound statement. No break allows fall-through to next case option.

. Last case option typically default.

Example:

3.18 Nested Flow of Control

Nest for, while, if and switch statements… See Boolean example…

3.19 The Conditional Operator

Syntax:

expr1 ? expr2 : expr3

Operation:

expr1 is evaluated; If expr1 is True (NZ), expr2 is evaluated, and val(expr2) is the conditional expression value. If expr1 is False (Z), expr3 is evaluated, and val(expr3) is the conditional expression value. Conditional expression type is ‘highest’ type between expr2 and expr3, i.e., if expr2 is int and expr3 is double, conditional expression type is double.

Example:

if (y < z)

x = y;

else

x=z;

could be rewritten as x = (y < z) ? y : z;

3.20 Style

Bell Labs Style…

1) Normal rules of English, i.e., space after a comma

2) Separate operators and operands for readability

3) Indent code for control flow readability

4) Use braces as:

for (i = 0; i < n; ++i) {

or

for (i = 0; i < n; ++i)

5) Nested if-else statements are ok with me…

3.21 Common Programming Errors

1) a = b vs a == b ?

2) infinite loops

3) unwanted semicolons = unwanted null statements

4) misuse of relational operators, (2 < k < 7) => (2 < k) && (k < 7).

3.22 System Considerations

Text says evaluation of (x < x + y) is implemented by (x – (x + y)) < 0.

Not True… Most machines will calculate (x + y) and then compare the result to x.