Docsity

Control Flow, Functions, and Program Structure in C - Prof. Douglas J. Wokoun, Study notes of Computer Science

Partial preview of the text

Download Control Flow, Functions, and Program Structure in C - Prof. Douglas J. Wokoun and more Study notes Computer Science in PDF only on Docsity!

Lecture 4

Control Flow, Functions, and

Program Structure

controlC is a structured programming language, which means that

fl ow (the path of active execution through a program)

design that:The idea of a code block is that it's a modular scope of programis mediated by code blocks.

Has a single, de

fi ned entry point

Has its own “local” variables

Has few, or one, exit point

This makes it easier to analyze the control

fl ow of a program

by breaking it down to small, understandable pieces.

processed.operations or sets of operations depending on the data beingBranching constructs direct the computer to alternateoperation or set of operations a certain number of times.Looping constructs direct the computer to perform anwhich means that they perform repetitive operations.Computers are used to manipulate large amounts of data,Blocks (cont.)

The If/Else

if

else

branching structure directs program

fl ow to

of an expression:one of t wo alternative statements based on the boolean value

?

true

false

If the expression evaluates true (non-zero), the

fi rst

statement is executed or the

fi rst block is entered. If the

executed.expression evaluates to zero, the second statement or block is

The syntax ofIf/Else (cont.)

if

else

is:

and ignored by the compiler.The parenthesis are required. The indenting is by convention

if (

expression

)

statement

else

statement

Examples ofIf/Else (cont.)

if

else

if (y == 1)

x = 0;

if (n)

b = 6;

{ else

c = 1;b = 5;

}

{ if (b - 4)

if (c == 5)

t = 12;

else}

b--;

True if n != 0

True if b != 4

if (y % 2)

if (x > 5)

w = 7;

else

v = 7;

True if y is odd

through chaining, theJust as other binary constructs can be extended to be n-aryIf/Else (cont.)

if

else

structure can be combined

binary division.to choose among several alternatives through repeated

?

true

false

?

true

false

?

true

false

The If/Else (cont.)

fi rst

if

else

instance divides bet ween case one and

subsequent divisions occur:“everything else.” Within the everything-else section,

if (y == 1)

x = 0;

else if (y == 2)

x = 2;

else if (y == 4)

x = 3;

else

x = 5;

evaluates true, then the associated block executes. Each expression in the chain gets evaluated until one

The Switch (cont.)

switch

switch ( block takes the following form:

expression

)

{

case

value

:

statement

case

value

:

statement

case

valuen

:

statement

default:

statement

}

The

default

section is optional.

The Switch (cont.)

switch

statement evaluates the value of the

expression and jumps directly to the

case

that has the same

value. If none of the

case

s match, any

default

case

A potentially confusing aspect of thepresent is executed.

switch

statement’s

through” to the next case unless you use abranching behavior is that execution of each case “falls

break

statement

to exit the

switch

statement.

And this code:Switch (cont.)

{ switch (mon) case JANUARY:

month = "January";

case FEBRUARY:

month = "February";

case MARCH:

month = "March";

case APRIL:

month = "April";

case MAY:

month = "May";

case JUNE:

month = "June";

case JULY:

month = "July";

case AUGUST:

month = "August";

case OCTOBER:case SEPTEMBER: month = "September";

month = "October";

case NOVEMBER:

month = "November";

case DECEMBER:

month = "December";

printf("%s\n", month);}

...will always print

December

regardless of the value of

mon

because month is repeatedly assigned (over written)

until it receives the

fi nal value of "

December

The way to correct this is to use theSwitch (cont.)

break

statement.

{ switch (mon) case JANUARY:

month = "January"; break;

case FEBRUARY:

month = "February"; break;

case MARCH:

month = "March"; break;

case APRIL:

month = "April"; break;

case MAY:

month = "May"; break;

case JUNE:

month = "June"; break;

case JULY:

month = "July"; break;

case AUGUST:

month = "August"; break;

case OCTOBER:case SEPTEMBER: month = "September"; break;

month = "October"; break;

case NOVEMBER:

month = "November"; break;

case DECEMBER:

month = "December"; break;

printf("%s\n", month);}

break

allows an escape from a block without reaching the

bottom.

Not strictly needed, but can prevent

errors if new cases are added later.

switch When dealing with a large number of match terms, theElse-If Versus Switch

syntax is a lot cleaner and more ef

fi cient.

Else

If

has the advantage that it can test against ranges

(e.g.,

x > 5

) and against variables (

x

n ). Cases within a

switch

statement are limited to integer constants or

compile time.expression that can be reduced to integer constants at

CPU’s assembly language, where the onlyThe behaviors of else-if and switch have their roots in theElse-If Versus Switch

fl ow control

mechanisms are forms of

goto

if (y == 1)

x = 0;

else if (y == 2)

x = 2;

else if (y == 4)

x = 3;

else

x = 5;

9: COMPARE #4,y8: JUMP 147: MOVE #2,x6: BRANCH-ON-NOT-EQUAL 95: COMPARE #2,y4: JUMP 143: MOVE #0,x2: BRANCH-ON-NOT-EQUAL 51: COMPARE #1,y

14:13: MOVE #5,x12: JUMP 1411: MOVE #3,x10: BRANCH-ON-NOT-EQUAL 13

Exit

(^) if

(^) block

Jump to

(^) fi rst else

Compiles to:

Jump to last else