chapter 4 control structures c programming for scientists & engineers with applications by reddy...

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

Upload: bertina-jean-gray

Post on 18-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

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

Chapter 4 Control Structures

C Programming for Scientists & C Programming for Scientists & Engineers with ApplicationsEngineers with Applications

by Reddy & Zieglerby Reddy & Ziegler

Page 2: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

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

Page 3: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 4: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 5: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

4.1 Relational and Logical Operations

Relational Operators and Relational Expressions

Logical Operator and Logical Expressions

Page 6: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 7: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Relational OperatorsRelational 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

Page 8: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 9: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 10: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 11: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

4.2 Selection Structures4.2 Selection Structures

Two-Way Selection StructuresTwo-Way Selection Structures Compound ConditionsCompound Conditions Multiway StructuresMultiway Structures

Page 12: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 13: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 14: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 15: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 16: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 17: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 18: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 19: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 20: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 21: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 22: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Conditional OperatorConditional 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 : zx = (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 Statements using the ? : operator are good shorthand for longer if-else type control structures.shorthand for longer if-else type control structures.

Page 23: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 24: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 25: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Logical ExpressionsLogical 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

Page 26: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 27: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 28: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 29: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 30: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 31: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 32: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 33: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 34: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Precedence of Precedence of OperatorsOperators

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)

Page 35: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

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 )

Page 36: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 37: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 38: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 39: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 40: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 41: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 42: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 43: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 44: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

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 }

Page 45: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 46: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 47: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 48: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 49: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

4.3 Repetition Structures4.3 Repetition Structures

Iterative LoopsIterative Loops Nested Iterative LoopsNested Iterative Loops Nested Iterative LoopsNested Iterative Loops Conditional LoopsConditional Loops

Page 50: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

While Loop: Part IWhile Loop: Part I

TopicsTopics Using while loopsUsing while loops

A test condition partA test condition part An execution partAn execution part

C provides a number of iterative control C provides a number of iterative control structures, known like structures, known like loopinglooping. .

The repeated execution of one or more The repeated execution of one or more statements.statements.

Page 51: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

While LoopWhile Loop The structure of a C The structure of a C while loopwhile loop::

while (while (expressionexpression))

{{

statement1statement1

statement2statement2

……

}} The The expressionexpression is a relational expression (variable, is a relational expression (variable,

constant, or an arithmetic expression) that results in constant, or an arithmetic expression) that results in either True or False.either True or False.

If the result is true, the If the result is true, the statementsstatements between the between the braces are executedbraces are executed

Page 52: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

A Generic IllustrationA Generic IllustrationCode before loop

{Block of Statements}

Loop keyword

Code after loop

yes

No

Page 53: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Source CodeSource Code #include <stdio.h>#include <stdio.h> 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 printf (" Loop number %d in the while

loop\n", i); loop\n", i); i++; i++; }} }}

Test expression

Incrementing counter variable

Statement block is repeatedly executed until test expression becomes false.

Page 54: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Do-While LoopsDo-While Loops

TopicsTopics Using do-while loopsUsing do-while loops Differences between do-while loops and Differences between do-while loops and

while loopswhile loops What is the structure of a do-while What is the structure of a do-while

loop?loop?

Page 55: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Do-while LoopsDo-while Loops What is the structure of a do-while loop?What is the structure of a do-while loop? dodo statementstatement whilewhile ( (expressionexpression););oror dodo {{ statement1;statement1; statement2;statement2; …… }} whilewhile ( (expressionexpression););

Page 56: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Do-while LoopsDo-while Loops The The statementsstatements between the braces are between the braces are

executed at least once regardless of whether executed at least once regardless of whether the the expressionexpression is True or False. is True or False. If the If the expressionexpression is True, the is True, the statementsstatements are are

executed again.executed again. What are the differences between do-while What are the differences between do-while

loops and while loops?loops and while loops? In the while loops, the test expression is tested In the while loops, the test expression is tested

first, and, if the test result is false, the loop body is first, and, if the test result is false, the loop body is not executed.not executed.

In the do-while loops, the loop body is executed In the do-while loops, the loop body is executed once. After that, if the test expression is False, the once. After that, if the test expression is False, the loop body is not executed again.loop body is not executed again.

Page 57: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

void

main(void)

{

{…}

{…}

{…}

{…}

……}

do

while

Page 58: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Source code L4_9.CSource code L4_9.C /*For Lesson 4_9 *//*For Lesson 4_9 */ #include <stdio.h> #include <stdio.h> 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 expressionbecomes false

Test expression is always false

Page 59: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Simple For LoopSimple For Loop TopicsTopics

The The forfor loop control structure loop control structure Structure of a simple Structure of a simple forfor loop loop Difference between Difference between for for loops and loops and whilewhile

loopsloops Appropriate when you know how many Appropriate when you know how many

times the operations needs to be times the operations needs to be repeated.repeated.

Page 60: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Simple For LoopSimple For Loop What is a What is a for loopfor loop??

The simplest for loopThe simplest for loop for (for (loop expressionsloop expressions)) single statement for_loop bodysingle statement for_loop body;; for (day = 1; day <= 3; day++)for (day = 1; day <= 3; day++) printf(printf(““Day = %2d\nDay = %2d\n””, day);, day);

The loop expression must consist three The loop expression must consist three parts:parts: An initialization expressionAn initialization expression A loop repetition conditionA loop repetition condition An increment expressionAn increment expression

Page 61: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Simple For LoopSimple For Loop

The general structure of a for loopThe general structure of a for loop for (for (loop_expressionloop_expression)) {{ for_loop_bodyfor_loop_body }} loop_body loop_body ExampleExample { { minutes = 60 * hour; minutes = 60 * hour; printf("Hour = %2d, Minutes=%3d\printf("Hour = %2d, Minutes=%3d\

n",hour, minutes); n",hour, minutes); }}

Page 62: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Simple For LoopSimple For Loop Difference between Difference between for for loops and loops and whilewhile loops loops

Item Item forfor loop loop whilewhile looploop

Initialization one of the loop expressions given prior to the loopInitialization one of the loop expressions given prior to the loop

Test expression one of the loop expressions one of the loop Test expression one of the loop expressions one of the loop expressions expressions

Increment expression one of the loop expressions must be in the loop Increment expression one of the loop expressions must be in the loop bodybody

The number of iteration convenient and clear less convenient and The number of iteration convenient and clear less convenient and clear clear

Is knowIs know

The number of iteration less convenient and clear more convenient and The number of iteration less convenient and clear more convenient and clear clear

Is unknow than Is unknow than forfor loop loop

Page 63: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

/*For Lesson 4_10 *//*For Lesson 4_10 */

#include <stdio.h>#include <stdio.h> 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, printf("Hour = %2d, Minutes=%3d\n",hour,

minutes); minutes); } } } }

initialization

Test expression

“Increment” expression

Body of for loop

Page 64: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

void

main(void)

{

Repetition condition

……}

An incrementexpression

for

initialization

No

yesyes

yes

Page 65: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Nested For LoopsNested For Loops The syntax of a nested for loop The syntax of a nested for loop

forfor (loop_1_expressions) (loop_1_expressions) {{ loop_body_1a /* optional loop_body for loop_body_1a /* optional loop_body for

outer loop */outer loop */ forfor (loop_2_expressions) /* this is an inner loop (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 loop_body_1b /* optional loop_body for

outer loop */outer loop */ } /* end of outer loop */} /* end of outer loop */

Page 66: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

Nested For LoopsNested For Loops How to make the code easy to read?How to make the code easy to read?

Indentation Indentation for (for (……) )

{{

……

……

for (for (……))

{{

……

……

}}

……

……

}}

Page 67: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

#include <stdio.h>#include <stdio.h> 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, printf("i=%3d, j=%3d, k=%3d\n", i,

j, k);j, k); } } m=k+i; m=k+i; } } } }

Inner loop

Outer loop

Page 68: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 69: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 70: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 71: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 72: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 73: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 74: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 75: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 76: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 77: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 78: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 79: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 80: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

4.4 Stacking and Nesting of Control Structures

Control Structure Stacking Nested Control Structures

Page 81: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 82: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 83: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 84: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 85: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 86: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 87: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 88: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 89: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler
Page 90: Chapter 4 Control Structures C Programming for Scientists & Engineers with Applications by Reddy & Ziegler

4.5 Sample Problems and 4.5 Sample Problems and ProgramsPrograms

Impedance and Inductance of an Impedance and Inductance of an Electrical CoilElectrical Coil

Altitude of a ProjectileAltitude of a Projectile Shear Stress of a Metallic MemberShear Stress of a Metallic Member Table of Periods of PendulumTable of Periods of Pendulum Compression Stress and Strain in Compression Stress and Strain in

Steel RodsSteel Rods Types of TrianglesTypes of Triangles