160 likes | 302 Views
E0001 Computers in Engineering. DO .... LOOP. Readings. Schneider Chapter 6 sections 6.1 and 6.2 - problems as required Schneider p 84 on This Lecture: two types PRETEST DO.. LOOPS. Assignment Documentation. Flowcharts OR pseudocode for main routine and ALL subs and functions
E N D
E0001 Computers in Engineering DO .... LOOP
Readings • Schneider Chapter 6 sections 6.1 and 6.2 - problems as required • Schneider p 84 on This Lecture: • two types PRETEST DO.. LOOPS
Assignment Documentation • Flowcharts OR pseudocode for main routine and ALL subs and functions • overviews for main, subs and functions • written paragraph/s stating what it does, how it does it, variables used from and returned to main routine • code with comments • screen dumps - ask in tut • write protected disk with code • flowcharts etc printed one staple in corner in folder
DO LOOP • loops repeat a set of statements - used when an UNKNOWN number if iterations is required • DO indicates the top of the loop LOOP indicates the end of the loop • DO without a LOOP and visa versa will give a run time error. Same rules for NESTED loops apply • four ways to write a DO LOOP • pre & post test; WHILE & UNTIL
Pre - test Loop condition tested before statements are executed DO WHILE condition statement (s) LOOP • statements executed if condition true DO UNTIL condition statement(s) LOOP • statements executed if condition false or UNTIL condition true
execute statements in loop true condition false Pretest WHILE loop flowchart DO WHILE condition loop statements LOOP
Pretest UNTIL loop flowchart execute statements in loop false condition true DO UNTIL condition loop statements LOOP
conditions • conditions use RELATIONAL and LOGIC operators • evaluated to TRUE or FALSE • if conditions are not met, the control passes to statement immediately after the LOOP • control value must be able to be changed in loop else infinite loop
sum =0 i=0 DO WHILE sum<1000 i = i+1 sum = sum +i LOOP PRINT “sum of first”;i ;”integers is”;sum ‘initalise sum to zero ‘as long as sum <1000 ‘set i to next integer and add to sum ‘repeat Example
sum =0 i=0 DO WHILE sum<1000 i = i+1 sum = sum +i LOOP PRINT “sum of first”;i ;”integers is”;sum sum =0 i=0 DO UNTIL ??????? i = i+1 sum = sum +i LOOP PRINT “sum of first”;i ;”integers is”;sum Write a corresponding DO UNTIL loop
sum =0 i=0 DO WHILE sum<1000 i = i+1 sum = sum +i LOOP PRINT “sum of first”;i ;”integers is”;sum sum =0 i=0 DO UNTIL sum>1000 i = i+1 sum = sum +i LOOP PRINT “sum of first”;i ;”integers is”;sum similar loop using UNTIL pretestequivalent condition
uses for DO LOOPS • to read or process an unknown number of data • used with ‘flags’ • re-run programs until user choice to finish
examples DO WHILE choice$<>“n” sum =0 i=0 DO WHILE sum<1000 i = i+1 sum = sum +i LOOP PRINT “sum of first”;i ;”integers is”;sum INPUT “continue?y/n”; choice$ LOOP original program nested loop
using flags (flag = 1) DO LOOP WHILE flag = 1 flag = 0 FOR i = 1 TO 19 bar(i) = (bar(i - 1) + bar(i) + bar(i + 1)) / 3 actualtoler = ABS(oldbar(i) - bar(i)) IF actualtoler >= tol THEN flag = 1 PRINT i, bar(i), oldbar(i), actualtoler oldbar(i) = bar(i) NEXT LOOP
process data; used with files and eof or eod markers eod - end of data marker in this case 999 sum =0: n=0 READ v DO WHILE v <> 999 sum = sum +v n = n+1 READ v LOOP average = sum/n DATA 21,22,23,25,4,55,67,999
eod with string data and multiple reads READ name$, address$, age DO WHILE name$ <> “eod” PRINT name$, address$,age total = total +age n = n+1 READ name$, address$, age LOOP PRINT “average age”;total/n DATA harry, 10 smith, 24, eod, 0, 0