DOC PREVIEW
MIT 16 01 - Introduction to Computers and Programming

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 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 12 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 12 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 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to Computers and Programming Lecture 11 Recap • visible and directly visible at each place within a program. The visibility rules apply to both explicit and implicit declarations. – immediate visibility and use-visibility • case selector is when value_list_1 => statement(s)_1;when value_list_2 => statement(s)_2; … when others => statement(s)_n;; Prof. I. K. Lundqvist Reading: FK pp. 199-209, 220-223, 266-267 Sept 29 2003Scope : determine which declarations are end caseIteration For Statement • for loop_var in loopstatement(s);; • for i in -1 .. 10 loopPUT(i); NEW_LINE;; for i in 1 .. 10 loopPUT(i); NEW_LINE;; for i in 2 .. n-1 loopPUT(i); NEW_LINE;; • Definite iteration – FOR statement • Indefinite iteration – WHILE statement – General LOOP statement low_val .. high_val end loopend loopend loopend loop[assignment_average.adb] [not covered in class] average number of assignments marked by period. marked per month, and display this value. • Specification – A program is required to calculate the a lecturer per month over a 12 month The program will ask the user for the number of assignments marked each month, calculate the average number Courtesy of Chris Lokan. Used with permission.[User interface] No. marked in month 1: xxx No. marked in month 2: xxx … No. marked in month 12: xxx Average per month is yyy.yy [Algorithm] • ASSIGNMENT AVERAGE PROGRAM • Initialization – Display heading – Set total to zero • Get values over year –For each month • Prompt for and get number marked • Add to total • Calculate average • Display average[Data Design] NAME TYPE Notes max_month (const) 12 No of months to process assgn_month Integer No of assignments in a month total_assgn Integer Total assignments in a year this_month Integer Loop parameter average_assgn Float Average assignment per month WHILE Statement • while test loopstatement(s);; Ex: millionaire.adb structure, to execute at least once • while (j < 0) loopput (“Enter positive j: );get (j); skip_line;; end loop• While loops may be designed as a repeat j := -1;end loopWHILE Statement tot := 0;PUT("Enter j (-1 to exit): ");GET(j); SKIP_LINE;while (j /= -1) looptot := tot + j;PUT("Enter j (-1 to exit): ");GET(j); SKIP_LINE;; PUT("Total is "); PUT(tot); NEW_LINE; While example 'y') to indicate yes, or 'N' (or 'n') to indicate the program confirms it and terminates. Any other response results in the prompting and input being repeated. You entered No. • A while loop may not execute at all end loop• Specification – The user is to be prompted to enter 'Y' (or no. If either of these responses is provided, •User interface – Please enter ‘Y’ for yes ‘N’ for no: X Please enter ‘Y’ for yes ‘N’ for no: NWhile example Algorithm Sentinel-controlled loops data – sentinel : constant := ???;loopread (item);item = sentinel;process the item;; • Do initialization – Set response to ‘x’ • Get yes no – While response is neither ‘Y’ nor ‘N’ • Prompt for and get response •Check input – Convert ‘y’ to ‘Y’ – Convert ‘n’ to ‘N’ • Confirm input – If response is ‘Y’ • Display yes confirmation – If response is ‘N’ • Display no confirmation • A sentinel value – A unique value that indicates end-of-data – It cannot be a value that could occur as exit when end loopFlag-controlled loops •A flag is a Boolean variable event has not yet occurred – flag : boolean;flag := false;while not flag loopdo some processing;if desired event has happened then flag := True;;do some processing;; Flag-controlled loops Example • while not DigitRead loopget (c);if (c >= ‘0’) and (c <= ‘9’) then DigitRead := TRUE;; ; – A value of False indicates that the desired – True indicates that it has occurred end ifend loopDigitRead := FALSE;end ifend loopWHILE vs. FOR while loop is most general test, can be made to execute at least once, or maybe never • For loops times to run before loop) while loop, but usually not as elegant WHILE vs. General LOOP • with general loops • – – executing the statements of the loop body • – – statements before the test (this may be the first time through the loop) •The – Used for indefinite iteration – By setting value of variables used in the – Used for definite iteration (know number of – Can be replaced by a It is easier to reason with while loops than While Obvious where loops exits Can guarantee the entry test has been passed when General loop Sometimes code is simpler There is no guarantee when executing theWhich loop statement to use? definite loops, use for known indefinite loops, where pre-test is natural, use while other indefinite loops, use general loop Loop control • any loop: – • • before • before a general loop, or in the statements before the exit test • continuation • termination – • • must •For – Automated control – Tells reader that number of iterations •For – Simpler reasoning •For – Less cluttered code • Usually prefer for and while There are three aspects to loop control for initialization of loop parameter(s) automatic in a for loop a while loop –test automatic in a for loop •while: test for general: test for update loop parameter not allowed inside for loop update inside indefinite loopLoop design • then specify the three aspects to loop control. Approaches to loop design: – – those situations (eg sentinel-controlled loops, flag-controlled loops) – – – – Nested loops times, and then do that multiple times. Choose the appropriate loop statement, and analyze the individual application recognize standard situations, use standard loops for use loop templates definite or indefinite? when to test for termination? what is appropriate initialization etc? • When want to do something multiple – Printing a 2-dimensional table – Printing the average mark of several exams • Programming process – Design and test inner loop – Design and test outer loopNested loops outer loop handles each row in turn; the inner loop handles the columns within each row. • • 1 2 3 4 5 6 7 8 9 10 1 1 2 3 4 5 6 7 8 9 10 2 2 4 6 8 10 12 14 16 18 20 ... 10 100 Nested loops • print a two-dimensional table: – the times table for the numbers 1-10. The User interface •


View Full Document

MIT 16 01 - Introduction to Computers and Programming

Documents in this Course
Fluids

Fluids

3 pages

Fluids

Fluids

4 pages

Fluids

Fluids

4 pages

Fluids

Fluids

5 pages

Load more
Download Introduction to Computers and Programming
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 Introduction to Computers and Programming 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 Introduction to Computers and Programming 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?