1 / 64

Control Structures in C Programming

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.

ghollins
Download Presentation

Control Structures in C Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control Structures Chapter 10 Chapter 10: Control Structures

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

  3. 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

  4. Syntax: if (expression) statement ; ‘if’ construct 'if’ construct

  5. Syntax: if (expression) { compound-statement ; } Compound-statement Action Compound-statement Action

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

  7. 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

  8. Example: Nested ‘if’ statement • Above could be rewritten as: Nested ‘if’ statement

  9. 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

  10. Example: Short-circuit evaluation Short-circuit evaluation

  11. 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

  12. 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

  13. Example: DeMorgan’s Theorem DeMorgan’s Theorem

  14. 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

  15. 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

  16. Wrong condition:   Common Mistakes • Correct method: which is equivalent to this (since <= has a higher precedence than &&): Common Mistakes

  17. Wrong condition:   Common Mistakes • Correct method: Common Mistakes

  18. Forgetting the braces for compound statements:   Common Mistakes • Correct method: Common Mistakes

  19. ‘if-else’ construct • Syntax: if (expression) statement1 ; else statement2 ; if (expression) { compound-statement1 ; } else { compound-statement2 ; } ‘if-else’ construct

  20.  ‘if-else’ construct • May be used to avoid redundant code: • Use ‘if-else’ construct: ‘if-else’ construct

  21. ‘if-else’ construct • Another example: ‘if-else’ construct

  22. Style • Two common styles: if (expression) { compound-statement1 ; } else { compound-statement2 ; } if (expression) { compound-statement1 ; } else { compound-statement2 ; } Style

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

  24. Removing common statements • After moving common statements out of ‘if’ construct: Removing common statements

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

  26. Logical assignment for Flags • Another example: Logical assignment for Flags

  27. Nested ‘if-else’ statements • Example: Nested ‘if-else’ statements

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

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

  30.  Nested ‘if-else’ statements • Example: Nested ‘if-else’ statements

  31. Nested ‘if-else’ statements • Example: Nested ‘if-else’ statements

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

  33. 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 (?:)

  34. Conditional Operator (?:) • Example: equivalent to: Conditional Operator (?:)

  35. Conditional Operator (?:) • Example: equivalent to: Conditional Operator (?:)

  36. ‘switch’ construct • Multi-way selection statement: ‘switch’ construct

  37. ‘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

  38. ‘switch’ construct • Example: ‘switch’ construct

  39. 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

  40. ‘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

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

  42. ‘while’ construct • Example: Compute sum of first 100 positive integers. ‘while’ construct

  43. ‘while’ construct • Which of these is/are same as previous code? ‘while’ construct

  44. ‘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

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

  46. Sentinel-control repetition • A sentinel is used to denote end of data. Sentinel-control repetition

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

  48. ‘do-while’ construct • Examples: ‘do-while’ construct

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

  50. ‘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

More Related