unit 5 same song, second verse programming loops introduction to c programming

18
Unit 5 “Same Song, Second Verse” Programming Loops Introduction to C Programming

Upload: thomas-corbett

Post on 27-Mar-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Unit 5

“Same Song, Second Verse”Programming Loops

Introduction toC Programming

Page 2: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Review of Unit 4

Unit 5: Review of Past Material

Page 3: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Review of Past ConceptsRelational, Equality, and Logical OperatorsSyntax of the Compound StatementSyntax of the if statementSyntax of the if-else structureThe switch statement

Data type of the selectorCase label syntax and data typeDefault label

Page 4: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Short-Circuit EvaluationIncrement/Decrement Operators

Unit 5: Advanced Operators

Page 5: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Short-Circuit EvaluationLogical "and" operator (&&)

The first operand is evaluatedThe second operand will not be evaluated if the first

is false

(4 > 7) && (x++ == 2)

Logical "or" operator (||)The first operand is evaluatedThe second operand will not be evaluated if the first

is true (4 > 3) && (x++ == 2)

Beware of "side-effects" not occurring as expected

Page 6: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Increment and Decrement OperatorsThe C increment operator is the "++"

operatorThe operator adds one to the variableint x = 4;

x = x + 1; /* Add one, x now is 5 */

++x; /* Same as above, x now is 6 */

To subtract one, use the decrement "--" operatorThe operator subtracts one from the variableint x = 4;

--x; /* Subtract one, x now is 3 */

Page 7: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Increment and Decrement OperatorsCan be combined into a larger expression

Prefix: the "++" or "--" comes before the variableThe variable is changed firstThe changed value is used in further calculations

int x = 4, y = 0;

y = ++x; /* x is 5, y is 5 */Postfix: the "++" or "--" comes after the

variableThe variable is changed lastThe value before change is used in further

calculations

int x = 4, y = 0;

y = x++; /* x is 5, but y is 4 */

Page 8: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Loop Structures

Unit 5: Loops

Page 9: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Basics of LoopsProgramming loops are iterative code structures

A set of instructions is repeated a number of timesLoops are controlled by a condition, or loop testA loop is preceded by an initialization

Sets up the loop condition for initial executionA loop contains an increment

Determines whether the loop continues or stops

Loops without a loop test repeat indefinitelyUseful for control systems or systems that "run

continuously"

Page 10: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Loop Structures - The "while" LoopLoop test is done first, at "top" of loopMay or may not execute once or more

int count;

count = 0; /* Initialization action */

while (count < 5) { /* Loop test (condition) */

/* Loop body - action to repeat */

++count; /* Loop increment */

}

Page 11: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Loop Structures - The "do-while" Loop

Loop test is done last, at "bottom" of loopAlways executes at least once

int count;

count = 0; /* Initialization action */

do {

/* Loop body - action to repeat */

++count; /* Loop increment */

} while (count < 5); /* Loop test (condition) */

Page 12: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Loop Structures - The "for" LoopThe "for" loop

Contains initialization, loop test, and increment all in one place

int count;

for (count = 0; count < 5; ++count) {

/* Loop body - action to repeat */

}

Page 13: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Different Loops for Different PurposesCounter-controlled loop

Loops a specific number of times as a counter increments

Conditional loopLoops until a specific condition becomes true

Sentinel-controlled loopProcesses data until a special value signals

the end of data

Page 14: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Modifying Loop ExecutionThe "break" statement break;

This is an immediate exit from a loopOnly used under unusual circumstances

The "continue" statement continue;Used to stop the current loop iterationBegins a new loop execution immediately

Page 15: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Principles of Structured Code

Unit 5: Structured Code

Page 16: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Structured CodeDesign principle for code

Entry at top of sectionExit at bottom of section

"Never" use the "goto" statementVery rare circumstances need "break" or

"continue" in loopAvoid "spaghetti code" - tangled, poorly

written code

Page 17: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Newton's Method

Unit 5: Loops

Page 18: Unit 5 Same Song, Second Verse Programming Loops Introduction to C Programming

Newton's Method for Finding RootsNewton's Method is an iterative methodFinds Roots (solutions) of a polynomial equation

Example codeFind root of f(x) = 3x^3 - 5x^2 + 2x +3There is a real root at x = -0.538962

Newton's Method starts with a "guess" (e.g. use -4)

Calculates iteratively better estimates of root using:X = oldX - f(oldX) / f '(oldX) where f ' is the

derivativeCompares oldX with new X estimate

When difference is very small, the estimates have "converged"