DOC PREVIEW
UW CSE 142 - Study Notes

This preview shows page 1-2-3-4-26-27-28-54-55-56-57 out of 57 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

University of Washington Computer Programming IOverviewPowerPoint PresentationAn Old Friend: Fahrenheit to CelsiusWhat’s “Wrong” with Fahrenheit/Celsius Program?One More Type of Control FlowLoopsSlide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Notes About Loop ConditionsCounting LoopsSlide 40Slide 41Slide 42for Loops vs while LoopsSlide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 58Slide 59H1-1University of WashingtonComputer Programming ILecture 9: Iteration© 2000 UW CSEH1-2OverviewConcepts this lectureIteration - repetitive executionLoops and nested loopswhile statementsfor statementsH1-3Chapter 5Read Sections 5.1-5.6, 5.105.1 Introduction5.2-5.3 While statement5.4 For statement5.5-5.6 Loop design5.7 Nested Loops5.11 Common errorsH1-4An Old Friend: Fahrenheit to Celsius#include <stdio.h>int main(void){double fahrenheit, celsius;printf("Enter a Fahrenheit temperature: ");scanf("%lf", &fahrenheit);celsius = (fahrenheit - 32.0) * 5.0 / 9.0;printf("That equals %f degrees Celsius.", celsius);return 0;}H1-5What’s “Wrong” with Fahrenheit/Celsius Program?User has to rerun the program for every new temperatureWouldn’t it be nice if the program could process repeated requests?Program ends immediately if user types a bad inputWouldn’t it be nice the program politely asked the user again (and again, etc. if necessary)?H1-6One More Type of Control FlowSometimes we want to repeat a block of code. This is called a loop.H1-7LoopsA “loop” is a repeated (“iterated”) sequence of statementsLike conditionals, loops (iteration) give us a huge increase in the power of our programsAlert: loops are harder to master than if statementsEven experienced programmers often make subtle errors when writing loopsH1-8Motivating LoopsProblem: add 4 numbers entered at the keyboard. int sum;int x1, x2, x3, x4;printf(“Enter 4 numbers: ”);scanf(“%d%d%d%d”, &x1, &x2, &x3, &x4);sum = x1 + x2 + x3 + x4;H1-9Motivating LoopsProblem: add 4 numbers entered at the keyboard. int sum;int x1, x2, x3, x4;printf(“Enter 4 numbers: ”);scanf(“%d%d%d%d”, &x1, &x2, &x3, &x4);sum = x1 + x2 + x3 + x4;This works perfectly! But... what if we had 14 numbers? or 40? or 4000?H1-10Finding Repeated CodeThe key to using loops to solve a problem is to discover steps that can be repeatedOur first algorithm for adding four numbers had no repeated statements at allBut it does have some repetition buried in it. Let’s rework the algorithm to make the repetition more explicitH1-11Add 4 Numbers, Repetitivelyint sum, x;sum = 0;printf(“Enter 4 numbers: “);scanf(“%d”, &x);sum = sum + x;scanf(“%d”, &x);sum = sum + x;scanf(“%d”, &x);sum = sum + x;scanf(“%d”, &x);sum = sum + x;H1-12Loop to Add 4 Numbersint sum, x;sum = 0;printf(“Enter 4 numbers:”);scanf(“%d”, &x);sum = sum + x;scanf(“%d”, &x);sum = sum + x;scanf(“%d”, &x);sum = sum + x;scanf(“%d”, &x);sum = sum + x;int sum, x;int count;sum = 0;printf(“Enter 4 numbers:”);count = 1;while (count <= 4) { scanf(“%d”, &x);sum = sum + x;count = count + 1;}H1-13More General Solutionint sum, x, count; int number_inputs; /* Number of inputs */sum = 0;printf(“How many numbers? “);scanf(“%d”, &number_inputs);printf(“Enter %d numbers: “, number_inputs);count = 1;while ( count <= number_inputs ) { scanf(“%d”, &x);sum = sum + x;count = count + 1;}H1-14while ( condition ) {statement1;statement2;...}while Statement SyntaxLoop body:Any statement, or a compound statementLoop conditionH1-15Compute 7!What is 1 * 2 * 3 * 4 * 5 * 6 * 7? (“seven factorial”)x = 1 * 2 * 3 * 4 * 5 * 6 * 7;printf ( “%d”, x ) ;H1-16Compute 7!What is 1 * 2 * 3 * 4 * 5 * 6 * 7? (“seven factorial”)x = 1 * 2 * 3 * 4 * 5 * 6 * 7;printf ( “%d”, x ) ;Bite size pieces: More Regular: As a loop:x = 1; x = 1; i = 2; x = 1;x = x * 2; x = x * i; i = i + 1; i = 2;x = x * 3; x = x * i; i = i + 1; while ( i <= 7 ) {x = x * 4; x = x * i; i = i + 1; x = x * i;x = x * 5; x = x * i; i = i + 1; i = i + 1;x = x * 6; x = x * i; i = i + 1; } x = x * 7; x = x * i; i = i + 1;H1-17 i <= 7 ? x = x * i ;i = i + 1 ;yesnowhile Loop Control Flowx = 1 ;i = 2 ;H1-19 /* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C */x = x * i ; /* D */i = i + 1 ; /* E */} /* F */ printf ( “%d”, x ) ; /* G */ Tracing the Loopline i xA ? 1H1-20 /* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C */x = x * i ; /* D */i = i + 1 ; /* E */} /* F */ printf ( “%d”, x ) ; /* G */ Tracing the Loopline i xA ? 1B 2 1H1-21 /* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C */x = x * i ; /* D */i = i + 1 ; /* E */} /* F */ printf ( “%d”, x ) ; /* G */ Tracing the Loopline i x i7?A ? 1B 2 1C 2 1 TH1-22 /* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C */x = x * i ; /* D */i = i + 1 ; /* E */} /* F */ printf ( “%d”, x ) ; /* G */ Tracing the Loopline i x i7?A ? 1B 2 1C 2 1 TD 2 2E 3 2H1-23 /* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C */x = x * i ; /* D */i = i + 1 ; /* E */} /* F */ printf ( “%d”, x ) ; /* G */ Tracing the Loopline i x i7?A ? 1B 2 1C 2 1 TD 2 2E 3 2C 3 2 TH1-24 /* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C */x = x * i ; /* D */i = i + 1 ; /* E */} /* F */ printf ( “%d”, x ) ; /* G */ Tracing the Loopline i x i7?A ? 1B 2 1C 2 1 TD 2 2E 3 2C 3 2 T......................C 7 720 TD 7 5040E 8 5040H1-25 /* What is 1 * 2 * 3 * ...*7 */x = 1 ; /* A */i = 2 ; /* B */while ( i <= 7 ) { /* C …


View Full Document

UW CSE 142 - Study Notes

Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?