Top Banner
Week 4 Kyle Dewey
56

Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Mar 31, 2015

Download

Documents

Alayna Thum
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Week 4Kyle Dewey

Page 2: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Overview

•New office hour location

•while / do-while / for loops

•break / continue

•Termination

•Exam recap

Page 3: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Office Hour

Page 4: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Motivation

•Factorial

•5! = 5 * 4 * 3 * 2 * 1 = 120

int factorial( int x ) { // return x! (x factorial)}

Page 5: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Factorial

•Need a way to say “for each integer from x down to 1, decrementing by 1 at a time”

5! = 5 * 4 * 3 * 2 * 1 = 120

Page 6: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

while

int x = 0;while ( x < 5 ) oneStatement();

while ( x < 5 ) { statementOne(); statementTwo();}

Page 7: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

while Semantics

Loop Loop BodyBody

Continue?Continue?

Rest of Rest of ProgramProgram

TrueFalse

Page 8: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #1

int x = 0;

while ( x < 5 ) { x++;}

•What does x equal at loop end?

Page 9: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #2

int x = 0;

while ( x < 5 ) { x = x + 2;}

•What does x equal at loop end?

Page 10: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #3

int x = 0;

while ( x < 5 ) { x = 10; printf( “moo” );}

•What does x equal at loop end?

•What does this print?

Page 11: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Factorial Revisited

•A lot of different ways to implement it

•One possible way:

•Track which number we are looking at in one variable

•Track the result in another

5! = 5 * 4 * 3 * 2 * 1 = 120

Page 12: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Factorial Revisitedint factorial( int fact ) { int result = fact; int num = fact - 1; while ( num > 1 ) { result = result * num; num--; }

return result;}

5! = 5 * 4 * 3 * 2 * 1 = 120

Page 13: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Factorial Revisitedint factorial( int fact ) { int result = 1; int num = 2; while( num <= fact ) { result = result * num; num++; }

return result;}

5! = 1 * 2 * 3 * 4 * 5 = 120

Page 14: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

do / while

Page 15: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

do / while

•Essentially a while loop with the condition at the end

•The body will always execute at least once

int x = 0;do { statementOne(); statementTwo();} while ( x > 0 );

Note semicolon!

Page 16: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #1

int x = 0;

do { x++;} while ( x < 5 );

•What does x equal at loop end?

Page 17: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #2

int x = 0;

do { x++;} while ( x < 0 );

•What does x equal at loop end?

int x = 0;

while ( x < 0 ) { x++;}

Page 18: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for

Page 19: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for

int x;

for ( x = 0; x < 50; x++ ) oneStatement( x );

for( x = 0; x < 50; x++ ) { statementOne(); statementTwo();}

Page 20: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for Semantics

Continue?Continue?

Loop Loop BodyBody

Inc / DecInc / Dec

InitializerInitializer

Rest of Rest of ProgramProgram

TrueFalse

Page 21: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Initializer

•Run before the loop starts

int x;

for ( x = 0; x < 50; x++ ) oneStatement( x );

InitializerInitializer Rest of Rest of LoopLoop

Page 22: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Compare

•Run before each iteration of the loop

int x;

for ( x = 0; x < 50; x++ ) oneStatement( x );

TerminatTerminate?e?

Loop Loop BodyBody

Rest of Rest of ProgramProgram

TrueFalse

Page 23: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Loop Body

•Run during each iteration of the loop

int x;

for ( x = 0; x < 50; x++ ) oneStatement( x );

TerminatTerminate?e?

Loop Loop BodyBody

Rest of Rest of ProgramProgram

TrueFalse

Page 24: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Increment / Decrement

•Run after each iteration of the loop

int x;

for ( x = 0; x < 50; x++ ) oneStatement( x );

Loop Loop BodyBody

Inc / DecInc / Dec

Page 25: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #1

int x;

for ( x = 0; x < 5; x++ ) { printf( “foobar” );}

•What does this print?

Page 26: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #2

int x;

for ( x = 0; x < 5; x++ ) { printf( “%i\n”, x );}

•What does this print?

Page 27: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #3

int x;int y = 0;

for ( x = 0; x < 5; x++ ) { y++;}

•What does y equal at loop end?

Page 28: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #4

int x;int y = 0;

for ( x = 1; x < 4; x++ ) { y++;}

•What does y equal at loop end?

Page 29: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #5

int x;int y = 0;

for ( x = 1; x % 3 != 0; x++ ) { y++;}

•What does y equal at loop end?

Page 30: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for Header

•It is not required to specify an increment, compare, or counter

•The semicolon still needs to be provided

•Can be tricker to read and understand

Page 31: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for Headerint x;

for ( x = 0; x < 5; x++ ) { printf( “moo” );}

•...is effectively the same as...int x = 0;

for ( ; x < 5; x++ ) { printf( “moo” );}

Page 32: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for Headerint x;

for ( x = 0; x < 5; x++ ) { printf( “moo” );}•...is effectively the same as...int x = 0;

for ( ; x < 5; ) { printf( “moo” ); x++;}

Page 33: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Factorial Revisited

•A lot of different ways to implement it

•One possible way:

•Track which number we are looking at in one variable

•Track the result in another

5! = 5 * 4 * 3 * 2 * 1 = 120

Page 34: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Factorial Revisitedint factorial( int fact ) { int result = fact; int num; for( num = fact - 1; num > 1; num-- ){ result = result * num; }

return result;}

5! = 5 * 4 * 3 * 2 * 1 = 120

Page 35: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Factorial Revisitedint factorial( int fact ) { int result = 1; int num; for( num = 2; num <= fact; num++ ){ result = result * num; }

return result;}

5! = 1 * 2 * 3 * 4 * 5 = 120

Page 36: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

break

•break can be used to immediately exit from a loop

•Can make things easier when used carefully

Page 37: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #1

int x = 0;while ( x < 5 ) { break;}

Page 38: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #2

int x = 0;while ( x < 5 ) { x = x + 2; if ( x >= 5 ) break; printf( “moo” );}

Page 39: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #3

int x;for ( x = 0; x < 5; x = x + 2 ) { if ( x >= 5 ) break; printf( “moo” );}

Page 40: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

continue

•continue causes the loop to skip the rest of the body

•Increments / decrements still performed for for loops

•Conditions will be rechecked

Page 41: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #1

int x = 0;while ( x < 10 ) { x++; continue; printf( “moo” );}

Page 42: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Example #2

int x;for( x = 0; x < 10; x++ ) { if ( x % 2 == 0 ) continue; printf( “%i\n”, x );}

Page 43: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Termination

•Consider the following:

while ( 1 ) { printf( “moo\n” );}

Page 44: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Termination

•Some loops may never terminate

•This may be desired

•...or not

Page 45: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Termination

•Useful example

char* command;

while ( 1 ) { printCommandPrompt(); command = readCommand(); executeCommand( command );}

Page 46: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Termination•Likely an error

•...or perhaps a clever way of checking for overflow and maximum int size

int x = 0;int y = 1;

while ( x < y ) { x++; y++;}

Page 47: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Infinite Loops

•Loops that never terminate are called infinite loops

•Usually in this context it is a bug

•Can be nasty to debug

•Complex termination conditions

•Is it just taking awhile?

Page 48: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Random Notes on Loops

Page 49: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Intentional Nontermination

•while loop:

while ( 1 ) { ... }

•for loop:

for ( ;; ) { ... }

Page 50: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for vs. while

•Each can be expressed in terms of the other

•Choose based on what seems more appropriate

Page 51: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

while as for

while ( x < 5 ) { ... }

•Can be represented as:

for (; x < 5;) { ... }

Page 52: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

for as while

int x;for ( x = 0; x < 5; x++ ) { ... }

•Can be represented as: (except for continue behavior!)

int x = 0;while ( x < 5 ) { ...; x++;}

Page 53: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Exam Recap

Page 54: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Difficulty Level

•Too hard?

•Too easy?

•Just right?

Page 55: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Grading•Hopefully by Tuesday, if not Tuesday

then Thursday

•Depending on how the grades turn out:

•No curve

•Curve

•Change weights

•Your grade can only improve with said changes

Page 56: Week 4 Kyle Dewey. Overview New office hour location while / do-while / for loops break / continue Termination Exam recap.

Exam Solutions