1 / 16

E0001 Computers in Engineering

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

trixie
Download Presentation

E0001 Computers in Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. E0001 Computers in Engineering DO .... LOOP

  2. Readings • Schneider Chapter 6 sections 6.1 and 6.2 - problems as required • Schneider p 84 on This Lecture: • two types PRETEST DO.. LOOPS

  3. 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

  4. 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

  5. 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

  6. execute statements in loop true condition false Pretest WHILE loop flowchart DO WHILE condition loop statements LOOP

  7. Pretest UNTIL loop flowchart execute statements in loop false condition true DO UNTIL condition loop statements LOOP

  8. 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

  9. 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

  10. 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

  11. 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

  12. uses for DO LOOPS • to read or process an unknown number of data • used with ‘flags’ • re-run programs until user choice to finish

  13. 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

  14. 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

  15. 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

  16. 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

More Related