Presentation is loading. Please wait.

# Introduction to C Programming

## Presentation on theme: "Introduction to C Programming"— Presentation transcript:

Introduction to C Programming
ET2560 Introduction to C Programming Introduction to C Programming Unit 5 “Same Song, Second Verse” Programming Loops Unit 1 Presentations

Review of Unit 4 Unit 5: Review of Past Material

Review of Past Concepts
Relational, Equality, and Logical Operators Syntax of the Compound Statement Syntax of the if statement Syntax of the if-else structure The switch statement Data type of the selector Case label syntax and data type Default label

Short-Circuit Evaluation Increment/Decrement Operators
Unit 5: Advanced Operators

Short-Circuit Evaluation
Logical "and" operator (&&) The first operand is evaluated The second operand will not be evaluated if the first is false (4 > 7) && (x++ == 2) Logical "or" operator (||) The 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 Operators
The C increment operator is the "++" operator The operator adds one to the variable int 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 "--" operator The operator subtracts one from the variable --x; /* Subtract one, x now is 3 */

Increment and Decrement Operators
Can be combined into a larger expression Prefix: the "++" or "--" comes before the variable The variable is changed first The 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 variable The variable is changed last The value before change is used in further calculations y = x++; /* x is 5, but y is 4 */

Loop Structures Unit 5: Loops

Basics of Loops Programming loops are iterative code structures
A set of instructions is repeated a number of times Loops are controlled by a condition, or loop test A loop is preceded by an initialization Sets up the loop condition for initial execution A loop contains an increment Determines whether the loop continues or stops Loops without a loop test repeat indefinitely Useful for control systems or systems that "run continuously"

Loop Structures - The "while" Loop
Loop test is done first, at "top" of loop May 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 loop Always 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" 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 Purposes
Counter-controlled loop Loops a specific number of times as a counter increments Conditional loop Loops until a specific condition becomes true Sentinel-controlled loop Processes data until a special value signals the end of data

Modifying Loop Execution
The "break" statement break; This is an immediate exit from a loop Only used under unusual circumstances The "continue" statement continue; Used to stop the current loop iteration Begins a new loop execution immediately

Principles of Structured Code
Unit 5: Structured Code

Structured Code Design principle for code
Entry at top of section Exit at bottom of section "Never" use the "goto" statement Very rare circumstances need "break" or "continue" in loop Avoid "spaghetti code" - tangled, poorly written code

Newton's Method Unit 5: Loops

Newton's Method for Finding Roots
Newton's Method is an iterative method Finds Roots (solutions) of a polynomial equation Example code Find root of f(x) = 3x^3 - 5x^2 + 2x +3 There is a real root at x = 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 derivative Compares oldX with new X estimate When difference is very small, the estimates have "converged"

Download ppt "Introduction to C Programming"

Similar presentations

Ads by Google