Chapter 10 Control Structures Chapter 10 Control Structures

  • Slides: 64
Download presentation
Chapter 10 Control Structures Chapter 10: Control Structures 1

Chapter 10 Control Structures Chapter 10: Control Structures 1

Flow of Control q Sequence q Selection q Repetition No action-1 condition Yes action-2

Flow of Control q Sequence q Selection q Repetition No action-1 condition Yes action-2 action Yes condition No Selection structure Chapter 10 Flow of Control Repetition structure 2

Selection: ‘if’ construct q Syntax: if (expression) statement ; q expression is the condition

Selection: ‘if’ construct q Syntax: if (expression) statement ; q expression is the condition for the ‘if’ construct. q If expression is evaluated to non-zero (true), statement is executed. q If expression is evaluated to zero (false), statement is skipped. Chapter 10 Selection: ‘if’ construct 3

‘if’ construct q Syntax: if (expression) statement ; Chapter 10 'if’ construct 4

‘if’ construct q Syntax: if (expression) statement ; Chapter 10 'if’ construct 4

Compound-statement Action q Syntax: if (expression) { compound-statement ; } Chapter 10 Compound-statement Action

Compound-statement Action q Syntax: if (expression) { compound-statement ; } Chapter 10 Compound-statement Action 5

Flags q Flag: an integer variable that simulates a Boolean variable. Contains 0 (false)

Flags q Flag: an integer variable that simulates a Boolean variable. Contains 0 (false) or 1 (true). or or Chapter 10 Flags 6

Naming a Flag q Appropriate naming is desirable. q Example: a flag ‘attended’ implies

Naming a Flag q Appropriate naming is desirable. q Example: a flag ‘attended’ implies u ‘attended’ if it contains 1 (true) u ‘did not attend’ if it contains 0 (false) Chapter 10 Naming a Flag 7

Nested ‘if’ statement q Example: q Above could be rewritten as: Chapter 10 Nested

Nested ‘if’ statement q Example: q Above could be rewritten as: Chapter 10 Nested ‘if’ statement 8

Short-circuit evaluation q Evaluation stops as soon as value of expression is known. Evaluation

Short-circuit evaluation q Evaluation stops as soon as value of expression is known. Evaluation is from left to right. q For logical AND (&&), if the partial expression is false, the whole expression is false. q For logical OR (||), if the partial expression is true, the whole expression is true. Chapter 10 Short-circuit evaluation 9

Short-circuit evaluation q Example: Chapter 10 Short-circuit evaluation 10

Short-circuit evaluation q Example: Chapter 10 Short-circuit evaluation 10

Complementing a Condition q Comlementing or negating a logical expression means changing the polarity.

Complementing a Condition q Comlementing or negating a logical expression means changing the polarity. q Examples: !(a == 30) is equivalent to (a != 30) !(a > b) is equivalent to (a <= b) Chapter 10 Complementing a Condition 11

De. Morgan’s Theorem q NOT(a AND b) same as NOT(a) OR NOT (b) NOT(a

De. Morgan’s Theorem q NOT(a AND b) same as NOT(a) OR NOT (b) NOT(a OR b) same as NOT(a) AND NOT(b) q In C: !(expr 1 && expr 2) same as !(expr 1) || !(expr 2) !(expr 1 || expr 2) same as !(expr 1) && !(expr 2) Chapter 10 De. Morgan’s Theorem 12

De. Morgan’s Theorem q Example: Chapter 10 De. Morgan’s Theorem 13

De. Morgan’s Theorem q Example: Chapter 10 De. Morgan’s Theorem 13

Common Mistakes q Do not use == and != on floating-point numbers. q Mixing

Common Mistakes q Do not use == and != on floating-point numbers. q Mixing up == with =. q Wrong placament of semi-colon, resulting in empty statement: printf() is outside ‘if’ construct. Chapter 10 Common Mistakes 14

Common Mistakes q Translating condition in English to C: q Let lower be 10,

Common Mistakes q Translating condition in English to C: q Let 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! Chapter 10 Common Mistakes 15

Common Mistakes q Wrong condition: q Correct method: which is equivalent to this (since

Common Mistakes q Wrong condition: q Correct method: which is equivalent to this (since <= has a higher precedence than &&): Chapter 10 Common Mistakes 16

Common Mistakes q Wrong condition: q Correct method: Chapter 10 Common Mistakes 17

Common Mistakes q Wrong condition: q Correct method: Chapter 10 Common Mistakes 17

Common Mistakes q Forgetting the braces for compound statements: q Correct method: Chapter 10

Common Mistakes q Forgetting the braces for compound statements: q Correct method: Chapter 10 Common Mistakes 18

‘if-else’ construct q Syntax: if (expression) statement 1 ; else statement 2 ; Chapter

‘if-else’ construct q Syntax: if (expression) statement 1 ; else statement 2 ; Chapter 10 if (expression) { compound-statement 1 ; } else { compound- statement 2 ; } ‘if-else’ construct 19

‘if-else’ construct q May be used to avoid redundant code: q Use ‘if-else’ construct:

‘if-else’ construct q May be used to avoid redundant code: q Use ‘if-else’ construct: Chapter 10 ‘if-else’ construct 20

‘if-else’ construct q Another example: Chapter 10 ‘if-else’ construct 21

‘if-else’ construct q Another example: Chapter 10 ‘if-else’ construct 21

Style q Two common styles: if (expression) { compound-statement 1 ; } else {

Style q Two common styles: if (expression) { compound-statement 1 ; } else { compound- statement 2 ; } Chapter 10 Style if (expression) { compound-statement 1 ; } else { compound- statement 2 ; } 22

Removing common statements q Common statements in the ‘then’ and ‘else’ parts should be

Removing common statements q Common statements in the ‘then’ and ‘else’ parts should be moved out of the ‘if’ construct, if appropriate: Chapter 10 Removing common statements 23

Removing common statements q After moving common statements out of ‘if’ construct: Chapter 10

Removing common statements q After moving common statements out of ‘if’ construct: Chapter 10 Removing common statements 24

Logical assignment for Flags q Example: ‘if-else’ statement may be replaced by an assignment

Logical assignment for Flags q Example: ‘if-else’ statement may be replaced by an assignment statement. Chapter 10 Logical assignment for Flags 25

Logical assignment for Flags q Another example: Chapter 10 Logical assignment for Flags 26

Logical assignment for Flags q Another example: Chapter 10 Logical assignment for Flags 26

Nested ‘if-else’ statements q Example: Chapter 10 Nested ‘if-else’ statements 27

Nested ‘if-else’ statements q Example: Chapter 10 Nested ‘if-else’ statements 27

Nested ‘if-else’ statements q Which ‘if’ is the ‘else’ associated with? q ‘else’ is

Nested ‘if-else’ statements q Which ‘if’ is the ‘else’ associated with? q ‘else’ is associated with nearest ‘if’. Chapter 10 Nested ‘if-else’ statements 28

Nested ‘if-else’ statements q To override default association, use braces to mark out block.

Nested ‘if-else’ statements q To override default association, use braces to mark out block. Chapter 10 Nested ‘if-else’ statements 29

Nested ‘if-else’ statements q Example: Chapter 10 Nested ‘if-else’ statements 30

Nested ‘if-else’ statements q Example: Chapter 10 Nested ‘if-else’ statements 30

Nested ‘if-else’ statements q Example: Chapter 10 Nested ‘if-else’ statements 31

Nested ‘if-else’ statements q Example: Chapter 10 Nested ‘if-else’ statements 31

Common Mistakes q Wrong matching of ‘else’ with ‘if’. q Wrong placement of semi-colon.

Common Mistakes q Wrong matching of ‘else’ with ‘if’. q Wrong placement of semi-colon. Chapter 10 Common Mistakes 32

Conditional Operator (? : ) q Ternary operator: condition ? expr 1 : expr

Conditional Operator (? : ) q Ternary operator: condition ? expr 1 : expr 2 q First operand is condition. q If condition is true, take value of expr 1; otherwise, take value of expr 2. Chapter 10 Conditional Operator (? : ) 33

Conditional Operator (? : ) q Example: equivalent to: Chapter 10 Conditional Operator (?

Conditional Operator (? : ) q Example: equivalent to: Chapter 10 Conditional Operator (? : ) 34

Conditional Operator (? : ) q Example: equivalent to: Chapter 10 Conditional Operator (?

Conditional Operator (? : ) q Example: equivalent to: Chapter 10 Conditional Operator (? : ) 35

‘switch’ construct q Multi-way selection statement: Chapter 10 ‘switch’ construct 36

‘switch’ construct q Multi-way selection statement: Chapter 10 ‘switch’ construct 36

‘switch’ construct q May only test constant integral expressions, i. e. , expressions that

‘switch’ construct q May only test constant integral expressions, i. e. , expressions that evaluate to integers or characters. q The vi’s are integral values; the si’s are compound statements. q After expression is evaluated, control jumps to appropriate ‘case’ label. q ‘break’ statements are inserted to avoid falling through. Chapter 10 ‘switch’ construct 37

‘switch’ construct q Example: Chapter 10 ‘switch’ construct 38

‘switch’ construct q Example: Chapter 10 ‘switch’ construct 38

Repetition Structure q Counter-controlled repetiton: number of iterations known. q Sentinel-controlled repetiton: iterate until

Repetition Structure q Counter-controlled repetiton: number of iterations known. q Sentinel-controlled repetiton: iterate until a sentinel value is entered, or terminating condition is true. Chapter 10 Repetition Structure 39

‘while’ construct q Loop structure with pre-test condition. while (expression) statement ; q expression

‘while’ construct q Loop structure with pre-test condition. while (expression) statement ; q expression is loop condition. q If expression is true, statement in loop body is executed, and expression tested again. q If expression is false, loop terminates. Chapter 10 ‘while’ construct 40

‘while’ construct q Example: Print n asterisks. q count_star is the loop control variable.

‘while’ construct q Example: Print n asterisks. q count_star is the loop control variable. Chapter 10 ‘while’ construct 41

‘while’ construct q Example: Compute sum of first 100 positive integers. Chapter 10 ‘while’

‘while’ construct q Example: Compute sum of first 100 positive integers. Chapter 10 ‘while’ construct 42

‘while’ construct q Which of these is/are same as previous code? Chapter 10 ‘while’

‘while’ construct q Which of these is/are same as previous code? Chapter 10 ‘while’ construct 43

‘while’ construct q Loop control variable. u Initialisation: before the loop is entered, the

‘while’ construct q Loop control variable. u Initialisation: before the loop is entered, the variable must be initialised. u Testing: condition involving the loop control variable is tested before the start of each loop iteration; if condition is true, loop body is executed. u Updating: loop control variable is updated during each iteration (usually at the beginning or the end of the loop body). Chapter 10 ‘while’ construct 44

Counter-control repetition q A counter is used to keep track of number of iterations.

Counter-control repetition q A counter is used to keep track of number of iterations. Chapter 10 Counter-control repetition 45

Sentinel-control repetition q A sentinel is used to denote end of data. Chapter 10

Sentinel-control repetition q A sentinel is used to denote end of data. Chapter 10 Sentinel-control repetition 46

‘do-while’ construct q Loop structure with post-test condition. do statement ; while (expression); q

‘do-while’ construct q Loop structure with post-test condition. do statement ; while (expression); q Loop body is executed at least once. Chapter 10 ‘do-while’ construct 47

‘do-while’ construct q Examples: Chapter 10 ‘do-while’ construct 48

‘do-while’ construct q Examples: Chapter 10 ‘do-while’ construct 48

Flag-controlled loops q When loop condition is complex, flags may be used. Chapter 10

Flag-controlled loops q When loop condition is complex, flags may be used. Chapter 10 Flag-controlled loops 49

‘for’ construct q Another pre-test loop structure. q Provides more compact form for countercontrolled

‘for’ construct q Another pre-test loop structure. q Provides more compact form for countercontrolled loops. for ( initialisation-expression; loop-condition; update-expression ) statement; Chapter 10 ‘for’ construct 50

‘for’ construct q The ‘for’ construct is similar to this ‘while’ construct. initialisation-expression; while

‘for’ construct q The ‘for’ construct is similar to this ‘while’ construct. initialisation-expression; while ( loop-condition ) { statement; update-expression; } Chapter 10 ‘for’ construct 51

‘for’ construct q Example: Chapter 10 ‘for’ construct 52

‘for’ construct q Example: Chapter 10 ‘for’ construct 52

‘for’ construct q The initialisation-expression and updateexpression are often comma-separated lists of expressions. q

‘for’ construct q The initialisation-expression and updateexpression are often comma-separated lists of expressions. q The comma operator evaluates the list from left to right. Chapter 10 ‘for’ construct 53

‘for’ construct q Any of the three expressions in the ‘for’ header may be

‘for’ construct q Any of the three expressions in the ‘for’ header may be omitted, but the semi-colons must stay. q If initialisation-expression is omitted, you must perform necessary initialisation before loop. Chapter 10 ‘for’ construct 54

‘for’ construct q If update-expression is omitted, you must ensure that necessary update operations

‘for’ construct q If update-expression is omitted, you must ensure that necessary update operations are done in loop body. Chapter 10 ‘for’ construct 55

‘for’ construct q If loop-condition is omitted, then the test is always true. q

‘for’ construct q If loop-condition is omitted, then the test is always true. q This loop is infinite: Chapter 10 ‘for’ construct 56

Common Mistakes q Wrong placement of semi-colon. Chapter 10 Common Mistakes 57

Common Mistakes q Wrong placement of semi-colon. Chapter 10 Common Mistakes 57

Common Mistakes q Omitting semi-colons in ‘for’ header. q Mixing up semi-colons with commas

Common Mistakes q Omitting semi-colons in ‘for’ header. q Mixing up semi-colons with commas in ‘for’ header. q Off-by-one error, where the loop executes one more or one fewer iteration than intended. How many iterations does this loop execute? Chapter 10 Common Mistakes 58

‘break’ statement q Used in loops, ‘break’ causes execution to break out of the

‘break’ statement q Used in loops, ‘break’ causes execution to break out of the loop that contains the statement. Chapter 10 ‘break’ statement 59

‘continue’ statement q The ‘continue’ statement causes execution to skip remaining loop body and

‘continue’ statement q The ‘continue’ statement causes execution to skip remaining loop body and proceed to next iteration. Chapter 10 'continue’ statement 60

Nested loops & combined structures q An example of nested ‘for’ loops. Chapter 10

Nested loops & combined structures q An example of nested ‘for’ loops. Chapter 10 Nested loops & combined structures 61

Nested loops & combined structures q Example 2: Chapter 10 Nested loops & combined

Nested loops & combined structures q Example 2: Chapter 10 Nested loops & combined structures 62

Nested loops & combined structures q Example 3: Chapter 10 Nested loops & combined

Nested loops & combined structures q Example 3: Chapter 10 Nested loops & combined structures 63

Homework Try exercises behind chapter 10. Chapter 10 Homework 64

Homework Try exercises behind chapter 10. Chapter 10 Homework 64