Download presentation
Presentation is loading. Please wait.
1
1 Chapter 3 Flow of Control
2
2 Review of Class on Sep 23
3
3 Outline of Chapter 3 Three types of flow of control How to specify conditions? Relational, Equality and Logical Operators
4
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 int main(void) { printf(“Hello, world!\n”); return 0; } hello.c
5
5 Introduction Flow of Control Sequential flow of control Statements in a program are executed one after another. Selection: select among alternative actions The condition to select a specific action Select: if, if-else, switch statement Iteraton: achieve iterative actions The condition to end the iterative action. Iteration: while, for, do statement
6
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 statement The switch statement Select using operator: The conditional Operator Achieve iterative actions The while statement The for statement The do statement Nested Flow of Control
7
7 Relational, Equality, and Logical Operators How true and false are implemented in C Representation of true and false false: represented by any zero value oint 0 ofloating 0.0 oNull character ‘\0’ oNull Pointer ( will be introduced in Chapter 8) true: represented by any nonzero value int int These operators yield either the int value 0 (false) or the int value 1 (true).
8
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
9
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==1 A =3 operator == is different from operator =
10
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)
11
11 Relational, Equality, and Logical Operators Precedence and associativity OperatorsAssociativity ++(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
12
12 Relational, Equality, and Logical Operators Summary Introduction True: non zero value False: zero value Relational Operators = Equality Operators == != Logical Operators ! && ||
13
13 End of Review
14
14 Class on Oct 06, Tuesday
15
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 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
16
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 statement An expression followed by a semicolon
17
17 Statements: compound statement and empty statement Examples of statement Examples: Expression: a =b Statement: a=b; Expression: a + b + c Statement: a+b+c; /* legal, but no useful work done */ Expression: printf(“%d\n”,a) Statement: printf(“%d\n”, a);
18
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; }
19
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);
20
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 Statement A single semicolon.
21
21 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 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
22
22 Introduction Weekday Weekend Long distance charges 8am to 5pm 5pm 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 Flow of Control: Select among alternative actions if (isweekday(day) && (time==11pm)) rate = nightrate; Decision Tree
23
23 The if and if-else Statement General form of if statement if (expr) statement next 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.
24
24 The if and if-else Statement General form of if-else statement if (expr) statement1 else 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.
25
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
26
26 The if and if-else Statement Example: #include 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.out x = 10, y = 11, min = 10
27
27 The if and if-else Statement Notes: expr is enclosed by parentheses If (expr) statement1 else statement2 If (expr) statement1
28
28 The if and if-else Statement #include 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
29
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) statement1 else statement2 if (expr) statement1 if (expr) { statements } else { statements } if (expr) { statements }
30
30 The if and if-else Statement #include 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) statement1 else statement2
31
31 The if and if-else Statement #include 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.c if2.c: In function `main': if2.c:9: error: parse error before "else" Example: if(expr) statement1 statement2 else statement3 statement4 if (expr) statement1 else statement2
32
32 The if and if-else Statement #include 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 x x = 10, y = 11, min = 10 Example: if (expr) statement1 statement2 if (expr) statement3 statement4 Statement1: Statement2: Statement3: Statement4:
33
33 The if and if-else Statement #include 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 y x = 10, y = 11, min = 10 if (expr) compound statement1 if (expr) compound statement2
34
34 The if and if-else Statement Example: #include int main(void) { int x = 10, y = 11; int min; if ( x<y ) { min = x; }; else{ min = y; } return 0; } if (expr) statement1 else statement2 % gcc if5.c if5.c: In function `main': if5.c:10: error: parse error before "else if (expr) statement1 If (expr) compound statement empty statement else compound statement
35
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 else statement2 If (expr) statement1
36
36 The if and if-else Statement Example if ( a==2 ) if ( b==2 ) printf(“Hi\n"); if (exp ) if statement 1 if (exp) statement2 If (expr) statement1 if(c==2) if ( a==2 ) if ( b==2 ) printf(“Hi\n"); if (c==2 ) if statement 1 if (a==2) if statement 2 if (b==2) printf(“Hi\n”);
37
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 statement If (exp) statement else statement if (exp ) else statement if statement If (exp) statement
38
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 else statement2 If (expr) statement1
39
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 statement If (exp) statement else statement if (exp ) else statement An if statement If (exp) statement
40
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 else statement2 if (expr) statement1
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.