70
1 Chapter 3 Flow of Control

1 Chapter 3 Flow of Control. 2 Outline How to specify conditions? Relational, Equality and Logical Operators Statements Statements: compound statement

  • View
    218

  • Download
    1

Embed Size (px)

Citation preview

Page 1: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

1

Chapter 3Flow of Control

Page 2: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

2

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 3: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

3

The switch Statement

multiway conditional statementGeneral form:

switch ( switch_exp ){ case constant_exp1: statements;

break; // optional

... case constant_expn: statements;

break; // optional default:

statements; break;

}

//optional

Page 4: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

4

The switch Statement

The effect of a switch: Evaluate the switch_exp. Go to the case label having a constant value

that matches the value of the switch_exp.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”.

Page 5: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

5

The switch Statement

switch ( switch_exp ){ case constant_exp1: statements;

break; // optional case constant_exp2 : statements; case constant_exp3 : statements; …../* no break */case constant_expi : statements;

break;……case constant_expn: statements;

break; // optional ……}Next statement;

Page 6: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

6

The switch Statement

switch ( switch_exp ){ case constant_exp1: statements;

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

break;……case constant_expn: statements;

break; // optional default:

statements; break;

}Next statement;

Page 7: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

7

The switch Statement

switch ( switch_exp ){ case constant_exp1: statements;

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

break;……case constant_expn: statements;

break; // optional }Next statement;

Page 8: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

8

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: break; } return 0;}

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

Page 9: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

9

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"); break; case 4: printf("case 4\n"); break; case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: break; } return 0;}

% gcc switch.c% a.outcase 3

Page 10: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

10

The switch StatementExample

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

% gcc switch.c% a.outcase 5

Page 11: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

11

The switch Statement

Rules: The expression in the parentheses following

the keyword switch ( switch_exp) must be of integer type.

The constant expression following the case labels must bean integer constant unique

Page 12: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

12

The switch StatementExample

The expression in the parentheses following the keyword switch must be of integer type.

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

% gcc switch1.cswitch1.c: In function `main':switch1.c:6: error: switch quantity not an integer%

Page 13: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

13

The switch StatementExample

The constant expression following the case labels must be

an integer constant.

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

% gcc switch2.cswitch2.c: In function `main':switch2.c:8: error: case label does not reduce to an integer constant

Page 14: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

14

The switch StatementExample

The constant expression following the case labels must all be unique

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

% gcc switch3.cswitch3.c: In function `main':switch3.c:9: error: duplicate case valueswitch3.c:8: error: previously used here

Page 15: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

15

The switch Statement

Summary The switch is a multiway conditional statement.

Evaluate the switch_exp.Go to the case label having a constant value

that matches the value of the switch_exp.o If a match is not found, go to the default label. o 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_exp must be of integer type case expressions must be of integer type and

must all be unique

Page 16: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

16

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 17: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

17

The Conditional Operator ?:

The general form of a conditional expression: 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.

Page 18: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

18

The Conditional Operator ?:

Examples: expr1? expr2: expr3 x=(y<z) ? y : z;

Page 19: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

19

The Conditional Operator ?:

Precedence and Associativity Precedence

Just above the assignment operators Associativity:

Right to left

…….

+ - Left to right

< <= > >= left to right

== != Left to right

….. ……

?: Right to left

= += -= *= /= etc. Right to left

Page 20: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

20

The Conditional Operator ?:

Examples:

int a=1, b=2;double x=7.07;

a == b ? a-1 : b+1…….

+ - LR

< <= > >= LR

== != LR

….. ……

?: RL

= += -= *= /= etc.

RL

( ) ( () )

a - b < 0 ? x : a + b( ) ( )

a - b > 0 ? x : a + b( ) ( )

( )

)(

Page 21: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

21

The Conditional Operator ?:

Summary: 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

Page 22: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

22

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 23: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

23

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

Page 24: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

24

The switch Statement

Summary The switch is a multiway conditional statement.

Evaluate the switch_exp.Go to the case label having a constant value

that matches the value of the switch_exp.o If a match is not found, go to the default label. o 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_exp must be of integer type case expressions must be of integer type and

must all be unique

Page 25: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

25

The Conditional Operator ?:

Summary: 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

Page 26: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

26

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 27: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

27

The while Statement

General formwhile (expr)

StatementNext 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.

Page 28: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

28

The while Statement

#include <stdio.h>int main(void){ int sum=0, i=1;

while(i<=3){ sum=sum+i; i=i+1; }

printf(“Sum=%d\n”,sum); return 0;}

expression: i<=3Relational operator <=This expression yields

1(true) or 0(false)

statement:sum=sum+i;i=i+1;

A group of statements enclosed between { and }.

Page 29: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

29

The while Statement

#include <stdio.h>int main(void){ int sum=0, i=1;

while(i<=3){ sum=sum+i; i=i+1; }

printf(“Sum=%d\n”,sum); return 0;}

sum=0i=1

sum=1i=2

exp (i<=3): (1<=3)=trueStatements are executed

sum=sum+i=0+1=1i=i+1=1+1=2exp (i<=3): (2<=3)=true

Statements are executed

sum=sum+i=1+2=3i=i+1=2+1=3

sum=3i=3 exp (i<=3): (3<=3)=true

Statements are executed

sum=sum+i=3+3=6i=i+1=3+1=4

sum=6i=4

exp (i<=3): (4<=3)=false while statement is done

Page 30: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

30

The while statement

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

Page 31: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

31

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 32: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

32

The for Statement

General form

Typically:expr1 is used to initialize the loop.expr2 is a logical expression controlling the

iteration.expr3 updates the variables used in expr2.

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

Page 33: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

33

The for Statement

Semantics: 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

Page 34: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

34

The for Statement

Semantically equivalent to

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

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

Page 35: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

35

The for Statement

Example

for (expr1; expr2; expr3)statement

Next statement

#include <stdio.h>int main(){ int k=14; int j; for (j=2; j<=k; ++j) { if (k % j == 0) printf("%d is a divisor of %d \n", j, k); } return 0;}

What is the output?

2 is a divisor of 147 is a divisor of 14

14 is a divisor of 14

Page 36: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

36

The for Statement

Note 1 for (expr1; expr2; expr3)

Semicolons are needed Example:

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

Incorrect

for (expr1; expr2; expr3)statement

Next statement

Page 37: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

37

The for Statement

Note 2 Any of all of the expressions in a for statement

can be missing, but the two semicolons must remain.If expr1 is missing, no initialization step is

performed as part of the for loopWhen expr2 is mission, the rule is that the

test is always true.

for (expr1; expr2; expr3)statement

Next statement

Page 38: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

38

The for Statement

Example of Note 2: expr1 is missing

No initialization step is performed as part of the for loop

i = 1;sum = 0;for (; i<=10; ++i)

sum +=i;

for (expr1; expr2; expr3)statement

Next statement

What the value of sum after the for loop?

Page 39: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

39

The for Statement

Example of Note 2: expr2 is missing

The test is always true.

i = 1;sum = 0;for (; ; ++i)

sum +=i;

for (expr1; expr2; expr3)statement

Next statement

How many iterations?

infinity

Page 40: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

40

The for Statement

Note 3 A for statement can be used as the statement

part of an if, if-else, while or another for statement

for (expr1; expr2; expr3)statement

Next statement

for (expr1; expr2; expr3)for(……)

for(……)Next statement

for (expr1; expr2; expr3)while(……)

if ……else……

Next statement

Page 41: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

41

The for Statement

Example of note 3

for (expr1; expr2; expr3)statement

Next statement

#include <stdio.h>int main(){ int k=14; int j; for (j=2; j<=k; ++j) { if (k % j == 0) printf("%d is a divisor of %d \n", j, k); } return 0;}

Page 42: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

42

The for Statement — Comma Operator

Example

Initialize two variablessum, i

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

sum+=i;

Can we put the initialization of these two variables in one expression?

Page 43: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

43

The for Statement — Comma Operator

The Comma Operator General Form: expr1, expr2

expr1 is evaluated first, then expr2.The comma expression as a whole has the

values and type of its right operand.

Page 44: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

44

The for Statement — Comma Operator

The Comma Operator Precedence : lowest Associativity: left to right Example

a=0,b=1o Value of this expression: 1o Type: int

Page 45: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

45

The for Statement — Comma Operator

The Comma Operator Comma operator can be used for

multiple initialization and multiple processing of indices in for statement

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

sum+=i;

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

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

;

Page 46: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

46

The for Statement — Comma Operator

Exampleint i, j, k=3;double x = 3.3

i = 1 , j = 2 , ++ k + 1

k != 7 , ++ x * 2.0 + 1

Precedence : lowest Associativity: left to right

( ) ( ) ( )( )

( )( )( )( )

Page 47: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

47

The for Statement

Summary The for Statement

The Comma Operator: expr1, expr2o expr1 is evaluated first, then expr2.o The comma expression as a whole has

the values and type of its right operand.

for (expr1; expr2; expr3)

statementNext statement

Page 48: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

48

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 49: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

49

The do Statement

General formdo

statementwhile (expr);Next statement

Typically, expr is a logical expression controlling the iteration.

Page 50: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

50

The do Statement

Semantics 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

Page 51: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

51

The do Statement

Example Suppose we want to read in an integer and

want to insist that the integer be positive.

do

statementWhile (expr);Next statement

do { printf(“Input a positive integer: “); scanf(“%d”, &n); if(error = (n<=0))

printf(“\n ERROR: Negative value not allowed!\n\n”);} while (error);

Page 52: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

52

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 53: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

53

The Break and Continue Statements

How to interrupt the normal flow of control? An exit from the innermost enclosing loop or switch

statement Causes the current iteration of a loop to stop and the

next iteration to begin immediately.

while (expr){

statement1;

Statement2;

Statement2;}next statement

Page 54: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

54

The Break and Continue Statements

break statement An exit from the innermost enclosing

loop (such as a while loop) statement or switch statement

Page 55: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

55

The Break and Continue Statementsbreak in a switch statement:

switch ( switch_exp ){ case constant_exp1: statements;

break; // optional case constant_exp2 : statements;

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

break; // optional default:

statements; break;

}Next statement;

//optional

Page 56: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

56

The Break and Continue Statementsbreak in a while statement

Example:A test for a negative argument is made,

and if the test is true, a break statement is used to pass control to the statement immediately following the loop.

while(1){scanf(“%1f”, &x);if(x<0.0)

break;printf(“%f\n”, sqrt(x));

}/* break jumps to hear */

Page 57: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

57

The Break and Continue Statements

continue statement Causes the current iteration of a loop to

stop and the next iteration to begin immediately.

while(exp){statements;continue; /* maybe in some if-statement*/

/* or switch statement */statements;

/* continue transfers control here to begin next iteration */}

Page 58: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

58

The Break and Continue Statements

Example Disregard small input value (-0.01 < x < 0.01).

while(cnt<n){scanf(“%1f”, &x);if(-0.01<x && x< +0.01)

continue;++cnt;sum += x;

/* continue transfers control here to begin next iteration */}

Page 59: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

59

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 60: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

60

Achieve iterative actions

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

Page 61: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

61

Achieve iterative actions

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

Page 62: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

62

Achieve iterative actions

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

Page 63: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

63

Achieve iterative actions

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.

Page 64: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

64

OutlineHow 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 statement

Nested Flow of Control

Page 65: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

65

Nested Flow of Control

Flow-of-control statement if, if-else switch for while do

Page 66: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

66

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

Page 67: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

67

Nested Flow of Control

while (expr)statement;

next statement

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

dostatement

while (expr); Next statement

Page 68: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

68

Nested Flow of Control

Flow-of-control statements can be nested within themselves and within one another.

Exampleif (expr)

statement1else

statement2if (expr) statement1

elsestatement2if (expr)

statement1else

statement2

Page 69: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

69

OutlineHow 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 statement

Nested Flow of Control

Page 70: 1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement

70

Chapter 3: Flow of Control

End of Chapter 3: Flow of Control