1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and...

Post on 19-Dec-2015

215 views 0 download

Transcript of 1 Review of Chapter 3--- Flow of Control How to specify conditions? Relational, Equality and...

1

Review of Chapter 3--- Flow of Control

How to specify conditions? Relational, Equality and Logical Operators

Statements Statements: compound statement and empty statement Select among alternative actions

The if and if-else statement The switch statements The conditional Operator

Achieve iterative actions The while statement The for statement The do statement The break and continue statements

Nested Flow of Control

2

Review of Chapter 3

Representation of true and false in C false: represented by any zero value true: represented by any nonzero value

Three types of operators Relational operators: <,>,<=,>= Equality Operators: == and != Logical operators:!, &&, and || These operators yield either the intint value 0

(false) or the intint value 1 (true).

3

Review of Chapter 3

statement Expression statement

An expression followed by a semicolon Compound Statement

A series of declarations and statements surrounded by braces.

Empty StatementA single semicolon.

4

Review of Chapter 3

if and if-else statement

exp is enclosed by parentheses Where appropriate, compound statements

should be used to group a series of statements under the control of a single if expression

An if or if-else statement can be used as the statement part of another if or if-else statement.an else attaches to the nearest if.

if (expr) statement1

elsestatement2

if (expr) statement1

5

Review of Chapter 3

switch statement Evaluate the

switch_cond. Go to the case label

having a constant value that matches the value of the switch_cond. If a match is not

found, go to the default label.

If there is no default label, terminate the switch.

Terminate the switch when a break statement is encountered, or by “falling off the end”.

switch ( switch_cond ){ case case_cond1: statements;

break; // optional

... case case_condn: statements;

break; // optional default:

statements; break;

}Next statement

//optional

6

The switch Statement

switch ( switch_cond ){ case case_cond1: statements;

break; // optional case case_cond2 : statements; case case_cond3 : statements; …../* no break */case case_condi : statements;

break;……case case_condn: statements;

break; // optional ……}Next statement;

7

The switch StatementExample

#include <stdio.h>int main(void){ int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: printf(“default\n”);break; } return 0;}

% gcc switch.c% a.outcase 3case 4case 5

8

The switch Statement

switch ( switch_cond ){ case case_cond1: statements;

break; // optional ……case case_condi : statements;

break;……case case_condn: statements;

break; // optional default:

statements; break;

}Next statement;

9

The switch StatementExample

#include <stdio.h>int main(void){ int x; x= 11; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: printf(“default\n”); break; } return 0;}

% gcc switch.c% a.outdefault

10

The switch Statement

switch ( switch_cond ){ case case_cond1: statements;

break; // optional ……case case_condi : statements;

break;……case case_condn: statements;

break; // optional }Next statement;

11

The switch StatementExample

#include <stdio.h>int main(void){ int x; x= 11; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; } return 0;}

% gcc switch.c% a.out%

12

Review of Chapter 3

switch Statement switch_cond must be of integer type case conditions must be of integer type and

must all be unique

13

Review of Chapter 3

Conditional Operator ?: General form: expr1? expr2: expr3 Semantics:

First, expr1 is evaluated. If it is nonzero (true), then expr2 is evaluated, and

this is the value of the conditional expression as a whole.

If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole.

Precedence: Just above the assignment operators Associativity: Right to left

14

Review of Chapter 3

The while statement

First expr is evaluated. If expr is nonzero (true), then statement is

executed and control is passed back to the beginning of the while loop.

Statement is repeatedly until expr is zero (false)

Then control passes to next statement.

while (expr)statement;

next statement

15

Review of Chapter 3

The for Statement First expr1 is evaluated. Then expr2 is evaluated.

If expr2 is nonzero (true), o then statement is executed,o expr3 is evaluatedo control passes back to the beginning of the for

loop again, except that evaluation of expr1 is skipped.

The process continues until expr2 is zero (false), at which point control passes to next statement.

for(expr1; expr2; expr3){

statement

} next statement

16

Review of Chapter 3

The do Statement

First statement is executed, and expr is evaluated.

If the value of expr is nonzero (true), then control passes back to the beginning of the do statement, and process repeats itself.

When expr is zero (false), then control passes to next statement

do

statementwhile (expr);Next statement

17

Review of Chapter 3

break statement An exit from the innermost enclosing loop or

switch statement

continue statement Causes the current iteration of a loop to

stop and the next iteration to begin immediately.

18

Review of Chapter 3

Statements Expression statement Compound Statement Empty Statement Flow-of-control statement

if, if-elseswitchforwhiledobreak, continue

19

Nested Flow of Control

switch ( switch_exp ){ case constant_exp1: statements;

break; // optional case constant_exp2 : statements;

break; // optional ... case constant_expn: statements;

break; // optional default:

statements; break;

}

//optional

if (expr) statement1

elsestatement2

if (expr) statement1

Expression statement Compound Statement Empty Statement Flow-of-control statement

if, if-else switch for while do break, continue

20

Nested Flow of Control

while (expr)statement;

next statement

for(expr1; expr2; expr3){ statement } next statement

dostatement

while (expr); Next statement

Expression statement Compound Statement Empty Statement Flow-of-control

statement if, if-else switch for while do break, continue

21

End of Review of Chapter 3

Read 3.1 – 3.22

22

Chapter 4:

Functions and Structured Programming

23

Introduction

An important feature of C: Structured Programming Language What is “Structured Programming”?

The construction of a program embodies top-down design.

o Decomposing a problem into smaller problems.

a collection of small problems or task.

24

Introduction

How C supports structured programming? The function construct is used to write code

that solves the small problems that result from the decomposition.

These functions are combined into other functions and ultimately used in main() to solve the original problem.

25

Outline of Chapter 4

How to write a function? Function Invocation Function Definition The return Statement Function Prototypes

More about function Function Declarations from the Compiler’s viewpoint Invocation and Call-by-Value Program Correctness: The assert() Macro

Top-Down Design

26

How to write a function?

An Example: prints a message In the main function, function prn_message is

called to print the following messageA message for you: Have a nice day!

How?How to give the compiler information about

the function?How to pass control to the function?How to specify the behavior of the function?How to get control back from the function?

27

How to write a function?

#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}

28

How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?

How to write a function?

#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}

Function prototype:

returns no values to the calling environment and

tells the compiler this function takes no arguments

29

How to write a function?

#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}

Function Invocation:The function is called

How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?

30

How to write a function?

#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}

Function Definition:The function is defined

How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?

31

How to write a function?

#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}

return statement:Control is passed back to the environment which calls the function

How to give the compiler information about the function?How to pass control to the function?How to specify the function?How to get the control back?

32

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

33

Function Invocation

#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: Have a nice day!\n”); return;}

A programis made up of one or more functions.One of them is function main()

How control is passed among

functions?

34

Function Invocation

How control is passed among functions?execution begins with main()When program control encounters a function name followed by parentheses,

the function is called, or invoked, which meansProgram control passes to the function

After the function does its work, program control is passed back to the calling environment, where program execution continues.

35

Function Invocation#include <stdio.h>void prn_message(void);int main(void){ prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void){ printf(“A message for you: “); printf(“Have a nice day!\n”); return;}

execution begins with main()When program control encounters a function name followed by parentheses,

the function is called, or invoked, which meanscontrol passes to the function

After the function does its work,

control is passed back to the calling environmentexecution continues.

36

Function Invocation

Function Invocation: A function is called in the following format:

a function name followed by parentheses Examples:

fun_name(); fun_name(argument1, argument2);

A function can be called in another function.

37

Function Invocation

#include <stdio.h>int min2(int a, int b);int min3(int a, int b, int c);int main(void){ printf("min of 11,23,24 is %d\n", min3(11,23,24));}

int min2(int a, int b){ if (a<b) return a; else return b;}int min3(int a, int b, int c){ int mofab = min2(a,b); return min2(mofab, c);}

38

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

39

Function Definition

Function Definition The C code that describes what a function does

#include <stdio.h>void prn_message(void);int main(void){ prn_message(); return 0;}void prn_message(void){ printf(“A message for you: Have a nice day!\n”); return;}

40

Function Definition — Example#include <stdio.h>void prn_message(int no_of_messages);int main(void){ int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many);}void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}

41

Function Definition

General form of function definitiontype function_name (parameter type list){

declarationsstatements

}

headerbody

void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}

header

body

declarations

statements

Example:

42

Function Definition

General form of function definition (cont’d)

Head: type of the function:

o the type of the value returned by the function.o void: nothing is returned.

type function_name (parameter type list){

declarationsstatements

}

header

void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}

43

Function Definition

General form of function definition (cont’d)

Head:Parameter type list:

o the number and types of the arguments that are passed into the function when it is invoked

o void: no arguments

type function_name (parameter type list){

declarationsstatements

}

header

void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}

44

Function Definition

General form of function definition (cont’d)

Head:Parameters in parameter type list (called as

formal parameters) o Identifiers, can be used within the function bodyo Upon invocation, the value of the argument

corresponding to a formal parameter is used within the function body.

type function_name (parameter type list){

declarationsstatements

}

header

void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}

45

Function Definition — Example#include <stdio.h>void prn_message(int no_of_messages);int main(void){ int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); return 0;}void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);}

46

Function Definition

General form of function definition

Body: specifies the behavior of a function.Upon invocation, the value of the

argument corresponding to a formal parameter is used within the function body.

type function_name (parameter type list){

declarationsstatements

}

headerbody

47

Function Definition

Summary Header:

Type: the type of the value returned by the function.

parameter type list: o the number and types of the arguments that are

passed into the function when it is called.o parameters, called formal parameter :

Identifiers, can be used within the function bodyUpon invocation, the value of the argument

corresponding to a formal parameter is used within the function body.

void: no arguments or no return value Body:specifies the behavior of a function.

type function_name (parameter type list){

declarationsstatements

}

48

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

49

The return Statement

When a return statement is executed, Program control is passed back to the

calling environment; If an expression follows the keyword return,

the value of the expression is returned.This value is converted, if necessary, to

the type of the function, as specified in the header to the function definition.

50

The return Statement

Two forms of a return statement: return; return expression;

Examples:o return 7;o return (a+b+c);

51

The return Statement#include <stdio.h>void prn_message(int no_of_messages);int main(void){ int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); return 0;}void prn_message(int no_of_messages){ int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n);} No return statement

52

The return Statement

There can be zero or more than one return statements in a function. Zero return statement: “falling off the end”

control is passed back when the closing brace is encountered.

More than one return statementOnce a return statement is executed,

control is passed back.

53

The return Statement#include <stdio.h>int min(int a, int b);int main(void){ int j, k, minimum; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); minimum = min(j,k); printf(“\n The minimum is %d.\n\n”, minimum); return 0;}int min(int a, int b){ if (a<b) return a; else return b;}

54

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

55

Function Prototype

Functions should be declared before they are used. Function prototype: tells the compiler

The number and type of arguments that are to be passed to the function

The type of the value that is to be returned by the function

main function is specialUsually, the programmer does not supply

a function prototype for main().

56

Function Prototypes

The general form of a function prototype type function_name(parameter type list);

Examples: int max(int a, int b);

type: intparameter type list: int a, int b

57

Function Prototypes

#include <stdio.h>int max(int a, int b);int main(void){ int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0;}int max(int a, int b){ if (a>b) return a; else return b;}

58

Function Prototypes

The general form of a function prototype type function_name(parameter type list); Parameter type list

A comma-separated list of typesIdentifiers are optional

o Identifiers are not used by the compiler;o Purpose: documentation

Example:o void f(char c, int i); is equivalent too void f(char, int);

59

Function Prototypes

#include <stdio.h>int max(int, int);int main(void){ int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0;}int max(int a, int b){ if (a>b) return a; else return b;}

60

Function Prototypes

Why function prototypes: All the compilers need to check the types of

values passed to a function. Values are coerced, where necessary. Example:

double f(double)Function call: f(4)

o 4 is promoted to a double

61

Function Prototypes— Why function prototypes

#include <stdio.h>int fun(int a);int main(void){ double a = 17.2; printf("fun(a)=%d\n", fun(a));

}int fun(int a){ return (a/3*3);}

fun(a)=15

62

Function Prototypes— Why function prototypes

#include <stdio.h>int fun(double a);int main(void){ double a = 17.2; printf("fun(a)=%d\n", fun(a));

}int fun(double a){ return (a/3*3);}

fun(a)=17

63

How to write a function?

#include <stdio.h>void prn_message(void);void prn_message(void);int main(void){ prn_message();prn_message(); printf(“Back to main function\n”); return 0;}void prn_message(void)void prn_message(void){{ printf(“A message for you: “);printf(“A message for you: “); printf(“Have a nice day!\n”);printf(“Have a nice day!\n”); return;return;}}

How to give the compiler information about the function?

Function prototype:How to pass control to the function?

Function InvocationHow to specify the function?

Function DefinitionHow to get the control back?

return statement

64

How to write a function?Preprocessing directivesfunction prototype of fucntion 1function prototype of fucntion 1int main(void){ Body of function def}Header of function1 def{

Body of function def}Header of function1 def{

Body of function def}

function invocations

65

How to write a function?

Question: write a code to computer the maximum value by using a function max()? Put its function prototype at the top of the

file Call the function as appropriate in main() Write its function definition at the bottom of

the file

66

The return Statement

#include <stdio.h> Preprocessing directivesFunction prototypeint main(void){ ……. …… Call function ……

}Header of function1 def{

Body of function def

}

int max(int a, int b);int main(void){ int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0;}int max(int a, int b){ if (a>b) return a; else return b;}

C code for calculating the maximum value

67

End of Class on Oct 12Read 4.1 to 4.5

68