640 likes | 664 Views
Learn about control structures, including sequence, selection, and repetition, in C programming. Understand the syntax and usage of if, if-else, nested if-else, and switch constructs. Avoid common mistakes and improve code readability.
E N D
Control Structures Chapter 10 Chapter 10: Control Structures
Sequence Selection Repetition No Yes condition Yes action-1 action-2 action condition No Repetition structure Selection structure Flow of Control Flow of Control
Syntax: if (expression) statement ; expression is the condition for the ‘if’ construct. If expression is evaluated to non-zero (true), statement is executed. If expression is evaluated to zero (false), statement is skipped. Selection: ‘if’ construct Selection: ‘if’ construct
Syntax: if (expression) statement ; ‘if’ construct 'if’ construct
Syntax: if (expression) { compound-statement ; } Compound-statement Action Compound-statement Action
Flag: an integer variable that simulates a Boolean variable. Contains 0 (false) or 1 (true). or or Flags Flags
Appropriate naming is desirable. Example: a flag ‘attended’ implies ‘attended’ if it contains 1 (true) ‘did not attend’ if it contains 0 (false) Naming a Flag Naming a Flag
Example: Nested ‘if’ statement • Above could be rewritten as: Nested ‘if’ statement
Evaluation stops as soon as value of expression is known. Evaluation is from left to right. For 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. Short-circuit evaluation Short-circuit evaluation
Example: Short-circuit evaluation Short-circuit evaluation
Comlementing or negating a logical expression means changing the polarity. Examples: !(a == 30) is equivalent to (a != 30) !(a > b) is equivalent to (a <= b) Complementing a Condition Complementing a Condition
NOT(a AND b) same as NOT(a) OR NOT (b) NOT(a OR b) same as NOT(a) AND NOT(b) In C: !(expr1 && expr2) same as !(expr1) || !(expr2) !(expr1 || expr2) same as !(expr1) && !(expr2) DeMorgan’s Theorem DeMorgan’s Theorem
Example: DeMorgan’s Theorem DeMorgan’s Theorem
Do not use == and != on floating-point numbers. Mixing up == with =. Wrong placament of semi-colon, resulting in empty statement: Common Mistakes printf() is outside ‘if’ construct. Common Mistakes
Translating condition in English to C: Common Mistakes • 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! Common Mistakes
Wrong condition: Common Mistakes • Correct method: which is equivalent to this (since <= has a higher precedence than &&): Common Mistakes
Wrong condition: Common Mistakes • Correct method: Common Mistakes
Forgetting the braces for compound statements: Common Mistakes • Correct method: Common Mistakes
‘if-else’ construct • Syntax: if (expression) statement1 ; else statement2 ; if (expression) { compound-statement1 ; } else { compound-statement2 ; } ‘if-else’ construct
‘if-else’ construct • May be used to avoid redundant code: • Use ‘if-else’ construct: ‘if-else’ construct
‘if-else’ construct • Another example: ‘if-else’ construct
Style • Two common styles: if (expression) { compound-statement1 ; } else { compound-statement2 ; } if (expression) { compound-statement1 ; } else { compound-statement2 ; } Style
Removing common statements • Common statements in the ‘then’ and ‘else’ parts should be moved out of the ‘if’ construct, if appropriate: Removing common statements
Removing common statements • After moving common statements out of ‘if’ construct: Removing common statements
Logical assignment for Flags • Example: ‘if-else’ statement may be replaced by an assignment statement. Logical assignment for Flags
Logical assignment for Flags • Another example: Logical assignment for Flags
Nested ‘if-else’ statements • Example: Nested ‘if-else’ statements
Nested ‘if-else’ statements • Which ‘if’ is the ‘else’ associated with? • ‘else’ is associated with nearest ‘if’. Nested ‘if-else’ statements
Nested ‘if-else’ statements • To override default association, use braces to mark out block. Nested ‘if-else’ statements
Nested ‘if-else’ statements • Example: Nested ‘if-else’ statements
Nested ‘if-else’ statements • Example: Nested ‘if-else’ statements
Common Mistakes • Wrong matching of ‘else’ with ‘if’. • Wrong placement of semi-colon. Common Mistakes
Conditional Operator (?:) • Ternary operator: condition ? expr1 : expr2 • First operand is condition. • If condition is true, take value of expr1; otherwise, take value of expr2. Conditional Operator (?:)
Conditional Operator (?:) • Example: equivalent to: Conditional Operator (?:)
Conditional Operator (?:) • Example: equivalent to: Conditional Operator (?:)
‘switch’ construct • Multi-way selection statement: ‘switch’ construct
‘switch’ construct • May only test constant integral expressions, i.e., expressions that evaluate to integers or characters. • The vi’s are integral values; the si’s are compound statements. • After expression is evaluated, control jumps to appropriate ‘case’ label. • ‘break’ statements are inserted to avoid falling through. ‘switch’ construct
‘switch’ construct • Example: ‘switch’ construct
Repetition Structure • Counter-controlled repetiton: number of iterations known. • Sentinel-controlled repetiton: iterate until a sentinel value is entered, or terminating condition is true. Repetition Structure
‘while’ construct • Loop structure with pre-test condition. while (expression) statement ; • expression is loop condition. • If expression is true, statement in loop body is executed, and expression tested again. • If expression is false, loop terminates. ‘while’ construct
‘while’ construct • Example: Print n asterisks. • count_star is the loop control variable. ‘while’ construct
‘while’ construct • Example: Compute sum of first 100 positive integers. ‘while’ construct
‘while’ construct • Which of these is/are same as previous code? ‘while’ construct
‘while’ construct • Loop control variable. • Initialisation: before the loop is entered, the variable must be initialised. • Testing: condition involving the loop control variable is tested before the start of each loop iteration; if condition is true, loop body is executed. • Updating: loop control variable is updated during each iteration (usually at the beginning or the end of the loop body). ‘while’ construct
Counter-control repetition • A counter is used to keep track of number of iterations. Counter-control repetition
Sentinel-control repetition • A sentinel is used to denote end of data. Sentinel-control repetition
‘do-while’ construct • Loop structure with post-test condition. do statement ; while (expression); • Loop body is executed at least once. ‘do-while’ construct
‘do-while’ construct • Examples: ‘do-while’ construct
Flag-controlled loops • When loop condition is complex, flags may be used. Flag-controlled loops
‘for’ construct • Another pre-test loop structure. • Provides more compact form for counter-controlled loops. for ( initialisation-expression; loop-condition; update-expression ) statement; ‘for’ construct