- 85 Views
- Uploaded on
- Presentation posted in: General

E0001 Computers in Engineering

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

E0001 Computers in Engineering

DO .... LOOP

- Schneider Chapter 6 sections 6.1 and 6.2 - problems as required
- Schneider p 84 on
This Lecture:

- two types PRETEST DO.. LOOPS

- 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

- 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

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

DO WHILE condition

loop statements

LOOP

execute

statements in

loop

false

condition

true

DO UNTIL condition

loop statements

LOOP

- 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

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

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

- to read or process an unknown number of data
- used with ‘flags’
- re-run programs until user choice to finish

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

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

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

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