Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
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.
4.1 Relational and Logical Operations Relational Operators and Relational Expressions Logical Operator and Logical Expressions
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
4.2 Selection Structures Two-Way Selection Structures Two-Way Selection Structures Compound Conditions Compound Conditions Multiway Structures Multiway Structures
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.
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
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)
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 )
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 }
4.3 Repetition Structures Iterative Loops Iterative Loops Nested Iterative Loops Nested Iterative Loops Conditional Loops Conditional Loops
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.
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
A Generic Illustration Code before loop {Block of Statements} Loop keyword Code after loop yes No
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.
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?
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);
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.
void main(void) { … {…}{…} {…}{…} {…}{…} {…}{…} ……}……} do while
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
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.
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
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); }
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
/*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
void main(void) { … Repetition condition ……}……} An increment expression for initialization No yes
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 */
Nested For Loops How to make the code easy to read? How to make the code easy to read? Indentation Indentation for ( … ) for ( … ) { … … { … … } … … }
#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
4.4 Stacking and Nesting of Control Structures Control Structure Stacking Nested Control Structures
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