Download presentation
Presentation is loading. Please wait.
Published byWesley Daniels Modified over 9 years ago
1
Chapter 10: Control Structures1 Chapter 10 Control Structures
2
Chapter 10Flow of Control2 qSequence qSelection qRepetition condition YesNo action-1action-2condition No action Yes Selection structure Repetition structure
3
Chapter 10Selection: ‘if’ construct3 qSyntax:if (expression) statement ; qexpression is the condition for the ‘if’ construct. qIf expression is evaluated to non-zero (true), statement is executed. qIf expression is evaluated to zero (false), statement is skipped.
4
Chapter 10'if’ construct4 ‘if’ construct qSyntax:if (expression) statement ;
5
Chapter 10Compound-statement Action5 qSyntax:if (expression) { compound-statement ; }
6
Chapter 10Flags6 qFlag: an integer variable that simulates a Boolean variable. Contains 0 (false) or 1 (true). or
7
Chapter 10Naming a Flag7 qAppropriate naming is desirable. qExample: a flag ‘attended’ implies u‘attended’ if it contains 1 (true) u‘did not attend’ if it contains 0 (false)
8
Chapter 10Nested ‘if’ statement8 qExample: qAbove could be rewritten as:
9
Chapter 10Short-circuit evaluation9 qEvaluation stops as soon as value of expression is known. Evaluation is from left to right. qFor logical AND (&&), if the partial expression is false, the whole expression is false. For logical OR (||), if the partial expression is true, the whole expression is true.
10
Chapter 10Short-circuit evaluation10 Short-circuit evaluation qExample:
11
Chapter 10Complementing a Condition11 Complementing a Condition qComlementing or negating a logical expression means changing the polarity. qExamples: !(a == 30) is equivalent to (a != 30) !(a > b) is equivalent to (a <= b)
12
Chapter 10DeMorgan’s Theorem12 DeMorgan’s Theorem qNOT(a AND b) same as NOT(a) OR NOT (b) NOT(a OR b) same as NOT(a) AND NOT(b) qIn C: !(expr1 && expr2) same as !(expr1) || !(expr2) !(expr1 || expr2) same as !(expr1) && !(expr2)
13
Chapter 10DeMorgan’s Theorem13 DeMorgan’s Theorem qExample:
14
Chapter 10Common Mistakes14 Common Mistakes qDo not use == and != on floating-point numbers. qMixing up == with =. qWrong placament of semi-colon, resulting in empty statement: printf() is outside ‘if’ construct.
15
Chapter 10Common Mistakes15 Common Mistakes qTranslating condition in English to C: qLet lower be 10, and upper be 30. If x is 20, (lower <= x) is true, so it is evaluated to 1. Since (1 <= upper) is also true, condition is true!
16
Chapter 10Common Mistakes16 Common Mistakes qWrong condition: qCorrect method: which is equivalent to this (since <= has a higher precedence than &&):
17
Chapter 10Common Mistakes17 Common Mistakes qWrong condition: qCorrect method:
18
Chapter 10Common Mistakes18 Common Mistakes qForgetting the braces for compound statements: qCorrect method:
19
Chapter 10‘if-else’ construct19 ‘if-else’ construct qSyntax: if (expression) statement1 ; else statement2 ; if (expression) { compound-statement1 ; } else { compound- statement2 ; }
20
Chapter 10‘if-else’ construct20 ‘if-else’ construct qMay be used to avoid redundant code: qUse ‘if-else’ construct:
21
Chapter 10‘if-else’ construct21 ‘if-else’ construct qAnother example:
22
Chapter 10Style22 Style qTwo common styles: if (expression) { compound-statement1 ; } else { compound- statement2 ; } if (expression) { compound-statement1 ; } else { compound- statement2 ; }
23
Chapter 10Removing common statements23 Removing common statements qCommon statements in the ‘then’ and ‘else’ parts should be moved out of the ‘if’ construct, if appropriate:
24
Chapter 10Removing common statements24 Removing common statements qAfter moving common statements out of ‘if’ construct:
25
Chapter 10Logical assignment for Flags25 Logical assignment for Flags qExample: ‘if-else’ statement may be replaced by an assignment statement.
26
Chapter 10Logical assignment for Flags26 Logical assignment for Flags qAnother example:
27
Chapter 10Nested ‘if-else’ statements27 Nested ‘if-else’ statements qExample:
28
Chapter 10Nested ‘if-else’ statements28 Nested ‘if-else’ statements qWhich ‘if’ is the ‘else’ associated with? q‘else’ is associated with nearest ‘if’.
29
Chapter 10Nested ‘if-else’ statements29 Nested ‘if-else’ statements qTo override default association, use braces to mark out block.
30
Chapter 10Nested ‘if-else’ statements30 Nested ‘if-else’ statements qExample:
31
Chapter 10Nested ‘if-else’ statements31 Nested ‘if-else’ statements qExample:
32
Chapter 10Common Mistakes32 Common Mistakes qWrong matching of ‘else’ with ‘if’. qWrong placement of semi-colon.
33
Chapter 10Conditional Operator (?:)33 Conditional Operator (?:) qTernary operator: condition ? expr1 : expr2 qFirst operand is condition. qIf condition is true, take value of expr1; otherwise, take value of expr2.
34
Chapter 10Conditional Operator (?:)34 Conditional Operator (?:) qExample: equivalent to:
35
Chapter 10Conditional Operator (?:)35 Conditional Operator (?:) qExample: equivalent to:
36
Chapter 10‘switch’ construct36 ‘switch’ construct qMulti-way selection statement:
37
Chapter 10‘switch’ construct37 ‘switch’ construct qMay only test constant integral expressions, i.e., expressions that evaluate to integers or characters. qThe v i ’s are integral values; the s i ’s are compound statements. qAfter expression is evaluated, control jumps to appropriate ‘case’ label. q‘break’ statements are inserted to avoid falling through.
38
Chapter 10‘switch’ construct38 ‘switch’ construct qExample:
39
Chapter 10Repetition Structure39 Repetition Structure qCounter-controlled repetiton: number of iterations known. qSentinel-controlled repetiton: iterate until a sentinel value is entered, or terminating condition is true.
40
Chapter 10‘while’ construct40 ‘while’ construct qLoop structure with pre-test condition. while (expression) statement ; qexpression is loop condition. qIf expression is true, statement in loop body is executed, and expression tested again. qIf expression is false, loop terminates.
41
Chapter 10‘while’ construct41 ‘while’ construct qExample: Print n asterisks. qcount_star is the loop control variable.
42
Chapter 10‘while’ construct42 ‘while’ construct qExample: Compute sum of first 100 positive integers.
43
Chapter 10‘while’ construct43 ‘while’ construct qWhich of these is/are same as previous code?
44
Chapter 10‘while’ construct44 ‘while’ construct qLoop control variable. uInitialisation: before the loop is entered, the variable must be initialised. uTesting: condition involving the loop control variable is tested before the start of each loop iteration; if condition is true, loop body is executed. uUpdating: loop control variable is updated during each iteration (usually at the beginning or the end of the loop body).
45
Chapter 10Counter-control repetition45 Counter-control repetition qA counter is used to keep track of number of iterations.
46
Chapter 10Sentinel-control repetition46 Sentinel-control repetition qA sentinel is used to denote end of data.
47
Chapter 10‘do-while’ construct47 ‘do-while’ construct qLoop structure with post-test condition. do statement ; while (expression); qLoop body is executed at least once.
48
Chapter 10‘do-while’ construct48 ‘do-while’ construct qExamples:
49
Chapter 10Flag-controlled loops49 Flag-controlled loops qWhen loop condition is complex, flags may be used.
50
Chapter 10‘for’ construct50 ‘for’ construct qAnother pre-test loop structure. qProvides more compact form for counter- controlled loops. for (initialisation-expression; loop-condition; update-expression ) statement;
51
Chapter 10‘for’ construct51 ‘for’ construct qThe ‘for’ construct is similar to this ‘while’ construct. initialisation-expression; while ( loop-condition ) { statement; update-expression; }
52
Chapter 10‘for’ construct52 ‘for’ construct qExample:
53
Chapter 10‘for’ construct53 ‘for’ construct qThe initialisation-expression and update- expression are often comma-separated lists of expressions. qThe comma operator evaluates the list from left to right.
54
Chapter 10‘for’ construct54 ‘for’ construct qAny of the three expressions in the ‘for’ header may be omitted, but the semi-colons must stay. qIf initialisation-expression is omitted, you must perform necessary initialisation before loop.
55
Chapter 10‘for’ construct55 ‘for’ construct qIf update-expression is omitted, you must ensure that necessary update operations are done in loop body.
56
Chapter 10‘for’ construct56 ‘for’ construct qIf loop-condition is omitted, then the test is always true. qThis loop is infinite:
57
Chapter 10Common Mistakes57 Common Mistakes qWrong placement of semi-colon.
58
Chapter 10Common Mistakes58 Common Mistakes qOmitting semi-colons in ‘for’ header. qMixing up semi-colons with commas in ‘for’ header. qOff-by-one error, where the loop executes one more or one fewer iteration than intended. How many iterations does this loop execute?
59
Chapter 10‘break’ statement59 ‘break’ statement qUsed in loops, ‘break’ causes execution to break out of the loop that contains the statement.
60
Chapter 10'continue’ statement60 ‘continue’ statement qThe ‘continue’ statement causes execution to skip remaining loop body and proceed to next iteration.
61
Chapter 10Nested loops & combined structures 61 Nested loops & combined structures qAn example of nested ‘for’ loops.
62
Chapter 10Nested loops & combined structures 62 Nested loops & combined structures qExample 2:
63
Chapter 10Nested loops & combined structures 63 Nested loops & combined structures qExample 3:
64
Chapter 10Homework64 Homework Try exercises behind chapter 10.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.