1 chapter 3 flow of control. 2 review of class on sep 23

40
1 Chapter 3 Flow of Control

Post on 22-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

1

Chapter 3Flow of Control

Page 2: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

2

Review of Class on Sep 23

Page 3: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

3

Outline of Chapter 3

Three types of flow of controlHow to specify conditions?

Relational, Equality and Logical Operators

Page 4: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

4

Introduction

Sequential flow of control Statements in a program are executed one

after another.

/*The traditional first program in honor of Dennis Ritchie who invented C at Bell Labs in 1972.*/

#include <stdio.h>int main(void){ printf(“Hello, world!\n”); return 0;}

hello.c

Page 5: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

5

Introduction

Flow of Control Sequential flow of control

Statements in a program are executed one after another.

Selection: select among alternative actionsThe condition to select a specific actionSelect: if, if-else, switch statement

Iteraton: achieve iterative actionsThe condition to end the iterative action.Iteration: while, for, do statement

Page 6: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

6

Outline of Chapter 3

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 statementThe switch statementSelect using operator: The conditional Operator

Achieve iterative actionsThe while statementThe for statementThe do statement

Nested Flow of Control

Page 7: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

7

Relational, Equality, and Logical Operators

How true and false are implemented in C Representation of true and false

false: represented by any zero valueo int 0o floating 0.0o Null character ‘\0’o Null Pointer ( will be introduced in Chapter 8)

true: represented by any nonzero valueThese operators yield either the intint value

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

Page 8: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

8

Relational Operators and Expressions

Relational operators: Four types of operators: <,>,<=,>= Operands: takes two expressions as operands Result: int value 0(false) or int value

1(true).Examples

1<1 1<=1 3>9 Variable: a<3 Complex Expression: -1.1>=(2.2*x+3.3)

false 0 true 1 false 0

Page 9: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

9

Equality Operators and Expressions

Equality Operators: == and != Operands: takes two expressions as operands Result: int value 0(false) or int value 1(true).

Examples: ‘A’ == ‘B’, ‘A’ != ‘B’ Variable: count !=-2 Complex expression: x+y == 2*z -5 int A = 2;

A==1A =3

operator == is different from

operator =

Page 10: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

10

Logical Operators

Logical negation !a Return 1 if a is zero value Otherwise return 0

a && b Return 1 (true) if both a and b are nonzero Otherwise return 0 (false).

a || b Return 0 (false) if both a and b are zero Otherwise return 1 (true)

Page 11: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

11

Relational, Equality, and Logical Operators

Precedence and associativity

Operators Associativity

++(postfix) –(postfix) Left to right

!,++(prefix),--(prefix),+(unary),-(unary)

Right to left

…… ……

+ - Left to right

Relational: < <= > >= Left to right

Equality: == != Left to right

Logical: && Left to right

Logical: || Left to right

Page 12: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

12

Relational, Equality, and Logical Operators

Summary Introduction

True: non zero valueFalse: zero value

Relational Operators< > <= >=

Equality Operators== !=

Logical Operators! && ||

Page 13: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

13

End of Review

Page 14: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

14

Class on Oct 06, Tuesday

Page 15: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

15

Outline

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 statementThe switch statementsThe conditional Operator

Achieve iterative actionsThe while statementThe for statementThe do statementThe break and continue statements

Nested Flow of Control

Page 16: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

16

Statements: compound statement and empty statement

Expression statement Expression:

meaningful combinations of constants, variables and function calls.

Most expressions have both a type (such as int, char, float number) and a value.

Expression statementAn expression followed by a semicolon

Page 17: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

17

Statements: compound statement and empty statement

Examples of statement Examples:

Expression: a =bStatement: a=b;

Expression: a + b + cStatement: a+b+c; /* legal, but no useful work done

*/

Expression: printf(“%d\n”,a)Statement: printf(“%d\n”, a);

Page 18: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

18

Statements: compound statement and empty statement

What is a compound statement? A series of declarations and statements

surrounded by braces. The chief use is to group statements into an

executable unit. Example:

{

a =1;{

b = 2;c = 3;

}

}

Page 19: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

19

Statements: compound statement and empty statement

What is empty statement? A single semicolon. Useful where a statement is required by the

syntax, but no action is required.in some situations of if-else and for

statement Example:

a =b;; /* an empty statement */printf(%d\n”,a);

Page 20: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

20

Statements: compound statement and empty statement

What is statement? Expression statement

An expression followed by a semicolon Compound Statement

A series of declarations and statements surrounded by braces.

Empty StatementA single semicolon.

Page 21: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

21

OutlineHow to specify conditions?

Relational, Equality and Logical OperatorsStatements

Statements: compound statement and empty statement

Select among alternative actionsThe if and if-else statementThe switch statementsThe conditional Operator

Achieve iterative actionsThe while statementThe for statementThe do statementThe break and continue statements

Nested Flow of Control

Page 22: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

22

Introduction

Weekday

Weekend

Long distancecharges

8am to 5pm5pm to 11pm

11pm to 8am

Sat.

Sun

evening rate

full rate

night rate

8am to 5pm

5pm to 11pm

11pm to 8am

evening rate

night rate

night rate

night rate

Flow of Control: Select among alternative actions

if (isweekday(day) && (time==11pm)) rate = nightrate;

Decision Tree

Page 23: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

23

The if and if-else Statement

General form of if statementif (expr)

statementnext statement

expr: usually is a relational, equality, or logical expression. (to specify the condition)

If expr is nonzero (true), then statement is executed; otherwise statement is skipped, and control passes to the next statement.

Page 24: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

24

The if and if-else Statement

General form of if-else statementif (expr)

statement1else

statement2 expr: usually is a relational, equality, or logical

expression. (to specify the condition) If expr is nonzero (true), then statement1 is

executed; if expr is zero (false), then statement1 is skipped and statement2 is executed.

Then control passes to the next statement.

Page 25: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

25

The if and if-else Statement

Example:

if (grade >= 90) printf(“Congratulations! \n”);

printf(“Your grade is %d. \n”, grade);

What is the output if grade = 91?Congratulations!Your grade is 91.

What is the output if grade = 89?Your grade is 89.

If (expr) statement1

Next statement

Page 26: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

26

The if and if-else Statement

Example:

#include <stdio.h>int main(void){ int x = 10, y = 11; int min; if ( x<y ) min = x; else min = y; printf("x = %d, y = %d, min = %d\n", x, y, min); return 0;}

% gcc if.c% a.outx = 10, y = 11, min = 10

Page 27: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

27

The if and if-else Statement

Notes: expr is enclosed by parentheses

If (expr) statement1

elsestatement2

If (expr) statement1

Page 28: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

28

The if and if-else Statement

#include <stdio.h>int main(void){ if 0==1 printf(" 0 is equal to 1 \n"); return 0;} % gcc if1.c

if1.c: In function `main7':if1.c:4: error: parse error before numeric constant

Example:

Condition expr should be enclosed by parentheses

Page 29: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

29

The if and if-else Statement

Notes: Where appropriate, compound statements

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

statement1else statement2

if (expr) statement1

if (expr){ statements}else{ statements}

if (expr) { statements}

Page 30: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

30

The if and if-else Statement#include <stdio.h>int main(void){ int x = 10, y = 11; int min; if ( x<y ) { min = x; printf(“x is min\n”); } else { min = y; printf(“y is min\n”); } printf("x = %d, y = %d, min = %d\n", x, y, min); return 0;}

if (expr) statement1else statement2

Page 31: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

31

The if and if-else Statement

#include <stdio.h>int main(void){ int x = 10, y = 11; int min; if ( x<y ) min = x; printf(" x is smalller than y \n"); else min = y; printf(" y is smalller than x \n");

printf("x = %d, y = %d, min = %d\n", x, y, min); return 0;}

% gcc if2.cif2.c: In function `main':if2.c:9: error: parse error before "else"

Example:

if(expr) statement1 statement2else statement3 statement4

if (expr) statement1else statement2

Page 32: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

32

The if and if-else Statement

#include <stdio.h>int main(void){ int x = 10, y = 11; int min; if ( x<y ) min = x; printf(" x is smaller than y \n"); if ( y<x ) min = y; printf(" y is smaller than x \n");

printf("x = %d, y = %d, min = %d\n", x, y, min); return 0;}

% gcc if3.c% a.out x is smaller than y y is smaller than xx = 10, y = 11, min = 10

Example:

if (expr) statement1

statement2if (expr)

statement3statement4

Statement1:Statement2:

Statement3:Statement4:

Page 33: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

33

The if and if-else Statement

#include <stdio.h>int main(void){ int x = 10, y = 11; int min; if ( x<y ) min = x; printf(" x is smaller than y \n"); if ( y<x ) min = y; printf(" y is smaller than x \n");

printf("x = %d, y = %d, min = %d\n", x, y, min); return 0;}

Example:

{

}

{

}

% gcc if3_correct.c% a.out x is smaller than yx = 10, y = 11, min = 10

if (expr) compound statement1if (expr) compound statement2

Page 34: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

34

The if and if-else StatementExample:

#include <stdio.h>int main(void){ int x = 10, y = 11; int min; if ( x<y ) { min = x; }; else{ min = y; } return 0;}

if (expr) statement1

elsestatement2

% gcc if5.cif5.c: In function `main':if5.c:10: error: parse error before "else

if (expr) statement1

If (expr) compound

statementempty statement

elsecompound

statement

Page 35: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

35

The if and if-else Statement

Notes: Because an if or if-else statement is itself a

statement, it can be used as the statement part of another if or if-else statement.

If (expr) statement1

elsestatement2

If (expr) statement1

Page 36: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

36

The if and if-else Statement

Example

if ( a==2 ) if ( b==2 ) printf(“Hi\n");

if (exp )

if statement 1if (exp) statement2

If (expr) statement1

if(c==2) if ( a==2 ) if ( b==2 ) printf(“Hi\n");

if (c==2 )

if statement 1if (a==2)

if statement 2if (b==2) printf(“Hi\n”);

Page 37: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

37

The if and if-else Statement

Example if ( a==2 ) if ( b==2 ) printf(“1\n");else printf(“2\n");

if (exp )

if-else statementIf (exp) statementelse statement

if (exp )

else statement

if statementIf (exp) statement

Page 38: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

38

The if and if-else Statement

Rule: When an if or if-else statement is 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

Page 39: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

39

The if and if-else Statement

Example if ( a==2 ) if ( b==2 ) printf(“1\n");else printf(“2\n");

if (exp )

An if-else statementIf (exp) statementelse statement

if (exp )

else statement

An if statementIf (exp) statement

Page 40: 1 Chapter 3 Flow of Control. 2 Review of Class on Sep 23

40

The if and if-else Statement

Summary

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