Tin học cơ sở 4$: Control Flow!
Tin học cơ sở 4$: Control Flow!
Control Flow!
Outline$
Making choices/Branching:!
The if construct! if-else! switch!
Repetition:!
while! do-while! for !
Phm Bo Sn 2
The if construct$
The if statement allows choice between two execution paths. One form:! ! ! if (expression){!
! ! ! ! statement! }!
statement is executed if the evaluation of expression is true.! statement is NOT executed if the evaluation of expression if false.! statement could be a single instruction, or a series of instructions enclosed in { } always use {}!
Phm Bo Sn 3
Phm Bo Sn
Phm Bo Sn
Style $
As you can see from the code examples, indentation is very important in promoting the readability of the code. ! Each logical block of code is indented.! Each { and } are indented to the appropriate logical block level.!
Style 1$ if(x)! {! statement;! }! Style 2 (preferred)$ if (x){! statement;! }!
For this course, we insist you always use curly braces even when there is only one statement inside. !
Phm Bo Sn 6
Complex if-else $
When you nest two or more if statements together:!
if (expression1)! ! if (expression2)! ! ! if (expression3)! ! ! statement1! ! ! else! ! ! statement2!
The rule is that the last else is associated with the closest previous if statement that does not have an else component.!
Phm Bo Sn 7
The else-if $
To create a multi-way decision chain:!
if (condition1) { ! ! ! statements1; !
Evaluates conditions until nds a True one! Then executes corresponding statements.! Then nishes if statement!
!
Phm Bo Sn 9
10
Conditional Expression $
Conditional expressions have the form:! ! ! expr1? expr2 : expr3! Typical usage:!
if (x < a){$ z = x;$ } else{$ z = a;$ }$ Equivalent to:$ z = (x < a)? x : a;$
Because it is an expression, it can be used whenever any expression are used. Use with caution!! You are advised to parenthesize expr1 because of precedence.!
Phm Bo Sn
11
Phm Bo Sn
13
Example of switch $
switch (month) {! case 2:! length = ( year%4 == 0 && ! ! (year%100!=0 || year%400==0))? 29: 28;! break;! case 4: case 6: case 9: case 11:! length = 30;! break;! default:! length = 31;! break; !
Phm Bo Sn 16
}!
Repetition $
C has several control structures for repetition:!
while: zero or more times ! do .. while: one or more times ! for: zero or more time with initialization and update. !
Phm Bo Sn
17
Repetition $
All repetition structures control:!
A single statement or! A block of statements in {}!
Repetition statements are also called loops.! The control statement(s) are called the loop body.!
Phm Bo Sn 18
Cu trc lp $
block + bool expr block + bool expr -
Phm Bo Sn
19
while example $
Compute the sum of the rst 50 positive integers:!
int sum, num;! sum = 0;! num = 1;! while (num <= 50){! sum = sum + num;! num = num + 1;! }!
Phm Bo Sn 21
init sets state for rst iteration, next sets state for next iteration.! Any of init, condition, or next may be omitted.! for is normally used for a xed number of iterations.! ! !
Phm Bo Sn 23
Example of for $
int n, i, factorial;! printf(n = );! scanf(%i , &n);! for (i = 1, factorial = 1; i <=n; i++){! factorial = factorial * i;! }! printf(%d ! = %d\n, n, factorial); !
Phm Bo Sn 24
Phm Bo Sn
25
References $
[K&R] Chapter 3.! [PHT] Chapter 3.1, 3.2!
Phm Bo Sn
26