Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.

Similar presentations


Presentation on theme: "Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler."— Presentation transcript:

1 Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

2 Control Structures Sequence Structure Selection Structure Want your programs to make decisions regarding which calculations to perform Repetition Structure May want to repeat calculations without writing the statement over and over. This is called looping.

3

4

5 4.1 Relational and Logical Operations Relational Operators and Relational Expressions Logical Operator and Logical Expressions

6

7 Relational Operators C contains six relational operators: < less than < = less than or equal to = = equal to > greater than > = greater than or equal to ! = not equal to

8

9

10

11 4.2 Selection Structures Two-Way Selection Structures Two-Way Selection Structures Compound Conditions Compound Conditions Multiway Structures Multiway Structures

12

13

14

15

16

17

18

19

20

21

22 Conditional Operator How does the ? : conditional operator work? How does the ? : conditional operator work? The ? : operator requires three operands. The ? : operator requires three operands. expression1 ? expression2 : expression3 expression1 ? expression2 : expression3 If expression1 is true, expression2 is executed. If expression1 is true, expression2 is executed. If expression1 is false, expression3 is executed. If expression1 is false, expression3 is executed. Can be used to find the smaller of two numbers. Can be used to find the smaller of two numbers. x = (y < z) ? y : z x = (y < z) ? y : z Assigns x the value of the smaller of y and z. Assigns x the value of the smaller of y and z. Statements using the ? : operator are good shorthand for longer if-else type control structures. Statements using the ? : operator are good shorthand for longer if-else type control structures.

23

24

25 Logical Expressions The results of a logical expression (using the symbols A and B to indicate relational expressions) A B A && B A | | B !A !B T T T T F F T F F T F T F T F T T F F F F F T T

26

27

28

29

30

31

32

33

34 Precedence of Operators What are the precedence and associativity of logical, relational, and arithmetic operators? Operator Name Associativity Precedence () parentheses L to R 1 (highest) ++, -- post-increment L to R 2 ++, -- pre-increment R to L 2 ! Logical NOT L to R 3 +, - Positive, negative sign L to R 3 +=,-=,*=,/=,%= compound assignment R to L 3 *, / multiplication, division L to R 4 +, - Addition, subtraction L to R 5 ==,>=,,<,!= relational operator L to R 6 && Logical AND L to R 7 || Logical OR L to R 8 = Assignment R to L 9 (lowest)

35 Precedence of Operators x = (a>b || b>c && a==b) x = ( a>b || b>c && a==b ) (6) x = ((a>b)) || (b>c) && (a==b)) x = ((4>-2) || (-2>0) && 4==-2) x = ( True || False && False ) (7) x = ( True || False ) (8) x = ( True )

36

37

38

39

40

41

42

43

44 Switch Control Structures What does the switch statement do? Commonly is constructed similarly to the if-else-if control structure. Transfer control Syntax switch(expression) { case constant1: statement1a statement1b … case constant2: statement2a statement2b … default: statements }

45

46

47

48

49 4.3 Repetition Structures Iterative Loops Iterative Loops Nested Iterative Loops Nested Iterative Loops Conditional Loops Conditional Loops

50 While Loop: Part I Topics Topics Using while loops Using while loops A test condition part A test condition part An execution part An execution part C provides a number of iterative control structures, known like looping. C provides a number of iterative control structures, known like looping. The repeated execution of one or more statements. The repeated execution of one or more statements.

51 While Loop The structure of a C while loop: The structure of a C while loop: while (expression) while (expression) { statement1 statement1 statement2 statement2 … } The expression is a relational expression (variable, constant, or an arithmetic expression) that results in either True or False. The expression is a relational expression (variable, constant, or an arithmetic expression) that results in either True or False. If the result is true, the statements between the braces are executed If the result is true, the statements between the braces are executed

52 A Generic Illustration Code before loop {Block of Statements} Loop keyword Code after loop yes No

53 Source Code #include #include void main(void) void main(void) { { int i; int i; i = 1; i = 1; while (i<= 5 ) while (i<= 5 ) { { printf (" Loop number %d in the while loop\n", i); printf (" Loop number %d in the while loop\n", i); i++; i++; } } } Test expression Incrementing counter variable Statement block is repeatedly executed until test expression becomes false.

54 Do-While Loops Topics Topics Using do-while loops Using do-while loops Differences between do-while loops and while loops Differences between do-while loops and while loops What is the structure of a do-while loop? What is the structure of a do-while loop?

55 Do-while Loops What is the structure of a do-while loop? What is the structure of a do-while loop? do statement while (expression); do statement while (expression);or do do { statement1; statement1; statement2; statement2; … } while (expression); while (expression);

56 Do-while Loops The statements between the braces are executed at least once regardless of whether the expression is True or False. The statements between the braces are executed at least once regardless of whether the expression is True or False. If the expression is True, the statements are executed again. If the expression is True, the statements are executed again. What are the differences between do-while loops and while loops? What are the differences between do-while loops and while loops? In the while loops, the test expression is tested first, and, if the test result is false, the loop body is not executed. In the while loops, the test expression is tested first, and, if the test result is false, the loop body is not executed. In the do-while loops, the loop body is executed once. After that, if the test expression is False, the loop body is not executed again. In the do-while loops, the loop body is executed once. After that, if the test expression is False, the loop body is not executed again.

57 void main(void) { … {…}{…} {…}{…} {…}{…} {…}{…} ……}……} do while

58 Source code L4_9.C /*For Lesson 4_9 */ /*For Lesson 4_9 */ #include #include void main(void) void main(void) { { int i=4, j=1; int i=4, j=1; do do { { printf("old_i=%2d, ",i); printf("old_i=%2d, ",i); i--; i--; printf("new_i=%2d\n",i); printf("new_i=%2d\n",i); } while(i); } while(i); do ++j; do ++j; while(j>999); while(j>999); printf("j=%2d\n",j); printf("j=%2d\n",j); } } Test expression Statement block is executed repeatedly until test expression becomes false Test expression is always false

59 Simple For Loop Topics Topics The for loop control structure The for loop control structure Structure of a simple for loop Structure of a simple for loop Difference between for loops and while loops Difference between for loops and while loops Appropriate when you know how many times the operations needs to be repeated. Appropriate when you know how many times the operations needs to be repeated.

60 Simple For Loop What is a for loop? What is a for loop? The simplest for loop The simplest for loop for (loop expressions) for (loop expressions) single statement for_loop body; single statement for_loop body; for (day = 1; day <= 3; day++) for (day = 1; day <= 3; day++) printf( “ Day = %2d\n ”, day); printf( “ Day = %2d\n ”, day); The loop expression must consist three parts: The loop expression must consist three parts: An initialization expression An initialization expression A loop repetition condition A loop repetition condition An increment expression An increment expression

61 Simple For Loop The general structure of a for loop The general structure of a for loop for (loop_expression) for (loop_expression) { for_loop_body for_loop_body } loop_body Example loop_body Example { minutes = 60 * hour; minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); printf("Hour = %2d, Minutes=%3d\n",hour, minutes); }

62 Simple For Loop Difference between for loops and while loops Difference between for loops and while loops Item for loop while loop Initialization one of the loop expressions given prior to the loop Test expression one of the loop expressions one of the loop expressions Increment expression one of the loop expressions must be in the loop body The number of iteration convenient and clear less convenient and clear Is know The number of iteration less convenient and clear more convenient and clear Is unknow than for loop

63 /*For Lesson 4_10 */ /*For Lesson 4_10 */ #include #include void main(void) void main(void) { int day, hour, minutes; int day, hour, minutes; for(day=1; day<=3; day++) for(day=1; day<=3; day++) printf("Day=%2d\n", day); printf("Day=%2d\n", day); for (hour=5; hour>2; hour--) for (hour=5; hour>2; hour--) { { minutes = 60 * hour; minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\n",hour, minutes); printf("Hour = %2d, Minutes=%3d\n",hour, minutes); } } } initialization Test expression “Increment” expression Body of for loop

64 void main(void) { … Repetition condition ……}……} An increment expression for initialization No yes

65 Nested For Loops The syntax of a nested for loop The syntax of a nested for loop for (loop_1_expressions) for (loop_1_expressions) { loop_body_1a /* optional loop_body for outer loop */ loop_body_1a /* optional loop_body for outer loop */ for (loop_2_expressions) /* this is an inner loop */ for (loop_2_expressions) /* this is an inner loop */ { /* start inner loop */ { /* start inner loop */ loop_body_2 /* loop_body for inner loop */ loop_body_2 /* loop_body for inner loop */ } /* end of inner loop */ } /* end of inner loop */ loop_body_1b /* optional loop_body for outer loop */ loop_body_1b /* optional loop_body for outer loop */ } /* end of outer loop */ } /* end of outer loop */

66 Nested For Loops How to make the code easy to read? How to make the code easy to read? Indentation Indentation for ( … ) for ( … ) { … … { … … } … … }

67 #include #include void main(void) void main(void) { { int i, j, k, m=0; int i, j, k, m=0; for (i=1; i<=5; i+=2) for (i=1; i<=5; i+=2) { { for (j=1; j<=4; j++) for (j=1; j<=4; j++) { { k = i+j;; k = i+j;; printf("i=%3d, j=%3d, k=%3d\n", i, j, k); printf("i=%3d, j=%3d, k=%3d\n", i, j, k); } } m=k+i; m=k+i; } } Inner loop Outer loop

68

69

70

71

72

73

74

75

76

77

78

79

80 4.4 Stacking and Nesting of Control Structures Control Structure Stacking Nested Control Structures

81

82

83

84

85

86

87

88

89

90 4.5 Sample Problems and Programs Impedance and Inductance of an Electrical Coil Impedance and Inductance of an Electrical Coil Altitude of a Projectile Altitude of a Projectile Shear Stress of a Metallic Member Shear Stress of a Metallic Member Table of Periods of Pendulum Table of Periods of Pendulum Compression Stress and Strain in Steel Rods Compression Stress and Strain in Steel Rods Types of Triangles Types of Triangles


Download ppt "Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler."

Similar presentations


Ads by Google