CSC 107 – Programming For Science. Today’s Goal Know how to use and write for loops Explain...
-
Upload
conrad-mcdonald -
Category
Documents
-
view
215 -
download
3
Transcript of CSC 107 – Programming For Science. Today’s Goal Know how to use and write for loops Explain...
LECTURE 13:FOR LOOPS
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 the 3 loops with same end
result Explain why we would never convert
between loops
Loop Structure
All loops can be divided into 2 parts:
LOOP BODY &
LOOP TERMINATION CONDITION
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
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
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
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
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!)
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…)
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!)
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)
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
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++){…}
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){ ... }
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;
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;
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;
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;
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++)
Your Turn
Get in groups & work on following activity
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