csc 107 – programming for science. today’s goal know how to use and write for loops explain...

21
LECTURE 13: FOR LOOPS CSC 107 – Programming For Science

Upload: conrad-mcdonald

Post on 19-Jan-2016

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

LECTURE 13:FOR LOOPS

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Today’s Goal

Know how to use and write for loops Explain why to use for, while, & do-while

loops Convert between the 3 loops with same end

result Explain why we would never convert

between loops

Page 3: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Structure

All loops can be divided into 2 parts:

LOOP BODY &

LOOP TERMINATION CONDITION

Page 4: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

while Loop

while (expression) { statement; ...}

expression is LOOP TERMINATION CONDITION Boolean expression checked at start of

each pass Executes entire loop body if expression is

true Once expression checked & found false,

loop is over LOOP BODY inside braces executed

during pass

Page 5: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

do-while Loop

do { statement; ...} while (expression);

expression is LOOP TERMINATION CONDITION Boolean expression checked at end of each

pass Re-executes entire loop body if expression

is true Once expression checked & found false,

loop is over LOOP BODY inside braces executed

during pass

Page 6: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Structure

All loops can be divided into 2 parts:

LOOP BODY &LOOP TERMINATION

CONDITION Explicit 3rd part used in for loops

LOOP CONTROL VARIANT

Page 7: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Control Variant

Variable used to control when loop ends Counting passes of loop often performed

with this Count not literal; could be constant form of

update Very common when used in strictly

bounded loops

Page 8: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Control Variant

Examples of loops using loop control variant Launching a rocket (10-9-8-7-6-5-4-3-2-1-

Blast off!)

Page 9: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Control Variant

Examples of loops using loop control variant Launching a rocket (10-9-8-7-6-5-4-3-2-1-

Blast off!) Going through even numbers (2-4-6-8…)

Page 10: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Control Variant

Examples of loops using loop control variant Launching a rocket (10-9-8-7-6-5-4-3-2-1-

Blast off!) Going through even numbers (2-4-6-8-10-

12-14…) Alarm clocks (5:57… 5:58… 5:59… $@#$@

it!)

Page 11: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Control Variant

Examples of loops using loop control variant Launching a rocket (10-9-8-7-6-5-4-3-2-1-

Blast off!) Going through even numbers (2-4-6-8-10-

12-14…) Alarm clocks (5:57… 5:58… 5:59… $@#$@

it!) Love (She loves me, she love me not, she

loves me)

Page 12: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

for Loop

for (initialization; expression; update) { statement; ...}

initialization run once before loop start

LOOP TERMINATION CONDITION is expression Boolean expression checked at start of

each pass Re-executes entire loop body if expression is

true Once expression found to be false, loop is

over

Page 13: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Initialization Expression

Executed only once before loop is entered

Semi-colon is required, but initialization optionalfor (; i <= 10; i++) { ... }

LOOP CONTROL VARIANT(S) declared & initialized Could declare earlier, but should not if only

for loopfor (int i=21; i < j; i+=4) { ... }

float ctr;for (ctr=0;ctr == sqrt(21);ctr++){…}

Page 14: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Loop Control Variant Update

Run at end of each pass of loop Will be executed only after loop body

completes Before loop termination condition is

checked Section can be used to update any

variable(s) Loop control variant or any other variable Really can be any statement you want

Like initialization section, this is optionalfor (double k=1; k!=64;) { ... }for (double k=1; k!=64; k*=2){ ... }

Page 15: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Comparing for & while Loops

for Loop while Loop

int i = 0, tgt = 4;for(int j = 1; j < tgt; j *= 2) { i += 1;}cout << "Log_2(";cout << tgt << ")="; cout << i << endl;

int i = 0,tgt = 4;int j = 1;while (j < tgt) { i += 1; j *= 2;}cout << "Log_2(";cout << tgt << ")="; cout << i << endl;

Page 16: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Tracing for Loop

int i = 0, tgt = 4;for(int j = 1; j < tgt; j *= 2) { i += 1;}cout << "Log_2(" << j << ")="; cout << i << endl;

Page 17: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Tracing for Loop

int i = 0, tgt = 4;for(int j = 1; j < tgt; j *= 2) { i += 1;}cout << "Log_2(" << j << ")="; cout << i << endl;

Page 18: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Tracing for Loop

int i = 0, tgt = 4;for(int j = 1; j < tgt; j *= 2) { i += 1;} cout << "Log_2(" << tgt << ")="; cout << i << endl;

Page 19: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Counting in for Loops

for's control variant often counts up or down Check against limit in loop termination

expression Limit against which we will check is often

constant Use constants to hold value of these

limits Makes code much easier to read Simplifies changing code when limit needs

to changefor (i = 0; i < 42; i++)

orfor (i = 0; i < THE_ANSWER; i++)

Page 20: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

Your Turn

Get in groups & work on following activity

Page 21: CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between

For Next Lecture

Midterm #1 next Monday & takes entire lecture Will be discussing this & reviewing term on

Friday No weekly assignment due to midterm Program Assignment #1 due Saturday at

11:59PM Been around for 1+ week, so should have

started