unit 5 same song, second verse programming loops introduction to c programming
TRANSCRIPT
Unit 5
“Same Song, Second Verse”Programming Loops
Introduction toC Programming
Review of Unit 4
Unit 5: Review of Past Material
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
Short-Circuit EvaluationIncrement/Decrement Operators
Unit 5: Advanced Operators
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
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 */
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 */
Loop Structures
Unit 5: Loops
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"
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 */
}
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) */
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 */
}
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
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
Principles of Structured Code
Unit 5: Structured Code
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
Newton's Method
Unit 5: Loops
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"