19
Chapter 3 Chapter 3 Control Flow Control Flow Ku-Yaw Chang Ku-Yaw Chang [email protected] [email protected] Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information Engineering Computer Science and Information Engineering Da-Yeh University Da-Yeh University

Chapter 3 Control Flow Ku-Yaw Chang [email protected] Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

Embed Size (px)

Citation preview

Page 1: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

Chapter 3Chapter 3

Control FlowControl Flow

Ku-Yaw ChangKu-Yaw [email protected]@mail.dyu.edu.tw

Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information EngineeringComputer Science and Information Engineering

Da-Yeh UniversityDa-Yeh University

Page 2: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

22Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.1 Statements and Blocks3.1 Statements and Blocks

An An expressionexpression such as such as x = 0 orx = 0 or i++ ori++ or printf(…)printf(…)

becomes a becomes a statementstatement when it is followed by a when it is followed by a semicolonsemicolon x = 0;x = 0; i++;i++; prinft(...);prinft(...);

In C, the semicolon is a statement terminator.In C, the semicolon is a statement terminator.

Page 3: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

33Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.1 Statements and Blocks3.1 Statements and Blocks

Braces { and } are used to group declarations Braces { and } are used to group declarations and statements together into a and statements together into a compound compound statementstatement, or , or blockblock.. Syntactically equivalent to a single statementSyntactically equivalent to a single statement No semicolon after the right brace that ends a blockNo semicolon after the right brace that ends a block

Page 4: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

44Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.2 If-Else3.2 If-Else

The if-else statement is used to express The if-else statement is used to express decisions.decisions.

if (expression)if (expression)

statementstatement11

elseelse

statementstatement22

The expression is evaluatedThe expression is evaluatedTrue : expression has a non-zero valueTrue : expression has a non-zero valueFalse: expression is zeroFalse: expression is zero

The else part is optionalThe else part is optional

Page 5: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

55Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.2 If-Else3.2 If-Else

Coding shortcutsCoding shortcutsif (expression)if (expression)

if (expression != 0)if (expression != 0)

Ambiguity when an else is omitted from a nested Ambiguity when an else is omitted from a nested if sequenceif sequenceifif (n > 0)(n > 0)

if (a > b)if (a > b)

z = a;z = a;

elseelse

z = b;z = b;

ifif (n > 0) {(n > 0) {if (a > b)if (a > b)

z = a;z = a;}}elseelse

z = b;z = b;

Page 6: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

66Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.3 Else-If3.3 Else-If

A multi-way decisionA multi-way decisionif (expression)if (expression)

statementstatement

else if (expression)else if (expression)

statementstatement

else if (expression)else if (expression)

statementstatement

else if (expression)else if (expression)

statementstatement

elseelse

statementstatement

The last else partThe last else part Non of the above or Non of the above or

default casedefault case Can be omittedCan be omitted To catch an impossible To catch an impossible

conditioncondition

Page 7: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

77Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.4 Switch3.4 Switch

A multi-way decisionA multi-way decisionTest whether an expression matches one of a Test whether an expression matches one of a number of number of constantconstant integer values, and integer values, and branches accordingly.branches accordingly.

switch (switch (expressionexpression) {) {case case const-exprconst-expr: : statementsstatementscase case const-exprconst-expr: : statementsstatementsdefault: default: statementsstatements

}}

Page 8: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

88Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.4 Switch3.4 Switch

switch (n) {switch (n) {case 1:case 1:

printf(“The number is 1\n.”);printf(“The number is 1\n.”);break;break;

case 2:case 2:printf(“The number is 2\n.”);printf(“The number is 2\n.”);break;break;

default:default:printf(“The number is not 1 or 2\n.”);printf(“The number is not 1 or 2\n.”);break;break;

}}

Page 9: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

99Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

Exercise 3-1Exercise 3-1

Write a program to count the occurrences Write a program to count the occurrences of each digits, while space, and all other of each digits, while space, and all other characters.characters.

Page 10: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1010Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.5 Loops – While and For3.5 Loops – While and For

WhileWhilewhile (while (expressionexpression))

statementstatement The expression is evaluatedThe expression is evaluated

Non-zero: statement is executed and expression is re-Non-zero: statement is executed and expression is re-evaluatedevaluated

The cycle continues until expression becomes zeroThe cycle continues until expression becomes zero

Page 11: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1111Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.5 Loops – While and For3.5 Loops – While and For

ForForfor (for (exprexpr11; ; exprexpr22; ; exprexpr33))

statementstatement

is equivalent tois equivalent toexprexpr11;;

while (while (exprexpr22) {) {

statementstatement

exprexpr33;;

}}

Any of the three parts can Any of the three parts can be omittedbe omitted

Semicolons must remainSemicolons must remain

An infinite loop:An infinite loop:for (;;) {for (;;) {

……

}} break or return break or return

Page 12: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1212Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

Exercise 3-2Exercise 3-2

Write a program that calculates and prints Write a program that calculates and prints the sum of the even integers from 2 to 30.the sum of the even integers from 2 to 30.

Page 13: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1313Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

Exercise 3-3Exercise 3-3

Write a function reverse(s) that reverses Write a function reverse(s) that reverses the string s in place.the string s in place. void reverse(char s[])void reverse(char s[])

Page 14: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1414Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3-6 Loops – Do-while3-6 Loops – Do-while

The syntax of the do isThe syntax of the do is

dodo

statementstatement

while (while (expressionexpression););

The body is always executed at least once.The body is always executed at least once. do-while is much less used than while and for.do-while is much less used than while and for.

Page 15: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1515Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.6 Loops – Do-while3.6 Loops – Do-while

/* itoa: convert n to characters in s *//* itoa: convert n to characters in s */void itoa(int n, char s[])void itoa(int n, char s[]){{

int i, sign;int i, sign;if ((sign=n) < 0)if ((sign=n) < 0) /* record sign *//* record sign */

n = -n;n = -n; /* make n positive *//* make n positive */i = 0;i = 0;do {do { /* generate digits in reverse order *//* generate digits in reverse order */

s[i++] = n % 10 + ‘0’;s[i++] = n % 10 + ‘0’; /* get next digit *//* get next digit */} while ((n /= 10) > 0);} while ((n /= 10) > 0); /* delete it *//* delete it */if (sign < 0)if (sign < 0) s[i++] = ‘-’;s[i++] = ‘-’;s[i] = ‘\0’;s[i] = ‘\0’;reverse(s);reverse(s);

}}

Page 16: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1616Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.7 Break and Continue3.7 Break and Continue

The break statement provides an early exit from The break statement provides an early exit from for, while, and do, just as from switch.for, while, and do, just as from switch.

A break causes the innermost enclosing loop or A break causes the innermost enclosing loop or switch to be exited immediately.switch to be exited immediately.

Page 17: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1717Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.7 Break and Continue3.7 Break and Continue

/* trim: remove trailing blanks, tabs, newlines *//* trim: remove trailing blanks, tabs, newlines */int trim(char s[])int trim(char s[]){{

int n;int n;

for (n = strlen(s)-1; n >= 0; n--)for (n = strlen(s)-1; n >= 0; n--)if (s[n] != ‘ ‘ && s[n] != ‘\t’ && s[n] != ‘\n’)if (s[n] != ‘ ‘ && s[n] != ‘\t’ && s[n] != ‘\n’)

break;break;s[n+1] = ‘\0’;s[n+1] = ‘\0’;return n;return n;

}}

Page 18: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

1818Ku-Yaw ChangKu-Yaw Chang Control FlowControl Flow

3.7 Break and Continue3.7 Break and Continue

The continue statement causes the next iteration The continue statement causes the next iteration of the enclosing for, while, or do loop to begin.of the enclosing for, while, or do loop to begin.

An example: skip negative valuesAn example: skip negative values

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

if (a[i] < 0) /* skip negative elements */if (a[i] < 0) /* skip negative elements */

continue;continue;

… … /* do positive elements *//* do positive elements */

}}

Page 19: Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

The EndThe End