E0001 computers in engineering
This presentation is the property of its rightful owner.
Sponsored Links
1 / 16

E0001 Computers in Engineering PowerPoint PPT Presentation


  • 76 Views
  • Uploaded on
  • Presentation posted in: General

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

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

Presentation Transcript


E0001 computers in engineering

E0001 Computers in Engineering

DO .... LOOP


Readings

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

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

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

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


Pretest while loop flowchart

execute

statements in

loop

true

condition

false

Pretest WHILE loop flowchart

DO WHILE condition

loop statements

LOOP


Pretest until loop flowchart

Pretest UNTIL loop flowchart

execute

statements in

loop

false

condition

true

DO UNTIL condition

loop statements

LOOP


Conditions

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


Example

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


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 ???????

i = i+1

sum = sum +i

LOOP

PRINT “sum of first”;i ;”integers is”;sum

Write a corresponding DO UNTIL loop


Similar loop using until pretest equivalent condition

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

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

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

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

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

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


  • Login