1 / 38

E0001 Computers in Engineering

E0001 Computers in Engineering. FOR ..NEXT Loops Arrays. Readings and exercises. Schneider Section 6.3 p 209 exercises 6.3 p 214 as required Module 12 from Study Book. For i = 1 to 4 x = x + i PRINT i, x NEXT. Output . i = i + 1. FOR NEXT loops. FOR i = 2 TO 10 STEP 3

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 FOR ..NEXT Loops Arrays

  2. Readings and exercises • Schneider Section 6.3 p 209 • exercises 6.3 p 214 as required • Module 12 from Study Book

  3. For i = 1 to 4 x = x + i PRINT i, x NEXT Output i = i + 1 FOR NEXT loops

  4. FOR i = 2 TO 10 STEP 3 x = x + i PRINT i, x NEXT output i = i + 3 FOR NEXT loops

  5. FOR NEXT LOOPS FOR controlvariable = low TO high [STEP increment] action action NEXT [controlvariable] FOR i = 1 to 10 STEP 2 FOR j = x to y PRINT i INPUT z x = x + i READ a NEXT NEXT

  6. assign starting values to variables that appear in the loop assign starting values loop variables FOR compare loop variable to final value loop variable greater loop variable =< execute loop statements NEXT Increment loop variable by step amount continue with program

  7. FOR i = 1 TO 10 PRINT i NEXT FOR i = 104 TO 345 STEP 3 PRINT i * i NEXT FOR i = 10 TO 1 STEP -1 PRINT I NEXT FOR I = 1 TO N c = c + 1 NEXT EXAMPLES

  8. Nested FOR NEXT LOOPS FOR var1 = low1 to high1 [STEP inc1] action.... FOR var2 = low2 to high2 [STEP inc] action.... NEXT action.. NEXT

  9. each iteration of outer loop requires complete execution of inner loop • QB allows for consolidation of NEXT clauses • total no. of times inner loop is executed is = (high1 - low1+1)(high2 - low2 +1).... (highn- lown+1)

  10. FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT complete the table

  11. FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT

  12. FOR NEXT Loops • used when know number of loop executions • used as element position in arrays • maybe nested • do not use EXIT FOR - > DO LOOP

  13. INDEPENDENT LOOPS FOR A.... --- --- NEXT A --- --- FOR b ---- ---- NEXT b NESTED LOOPS FOR a --- --- FOR b ---- ---- NEXT b --- --- NEXT a

  14. crossed loops FOR a -- -- FOR b -- NEXT a NEXT b nested-same counter FOR a -- FOR a ---- NEXT a --- NEXT a ILLEGAL LOOPS

  15. Rainfall for the Month of February • individual variable names

  16. Use Arrays • set up a variable called Feb • allocate 28 storage locations to that variable • into each location put the rainfall for that day

  17. DIM Feb(28) FOR I= 1 TO 28 INPUT Feb(I) NEXT ‘sets aside variable Feb with 28 locations ‘use variable I specify each position of Feb ‘input numbers into each position ‘endloop The Process

  18. Add to the Program FOR I = 1 TO 28 TOTAL = TOTAL + Feb (I) NEXT PRINT “total rainfall for February = “; TOTAL amt = Feb (1) + Feb (10) ‘use array element as ordinary variable

  19. FOR I = 1 TO 3 FOR J = 1 TO 3 action NEXT NEXT Make use of i and j

  20. 2 Dimensional Array Transpose into matrix I - ROW J- COLUMN

  21. 2 dimensional arrays • set aside memory; 2 dimensions; number of rows and columns • name matrix as a variable - X e.g. DIM X (3,3) • elements specified by subscripts for row & column (I, J) e.g. X(I,J) or X$(2,3)

  22. DIM a(3,3) ‘allocate storage FOR i = 1 to 3 ‘i and j as subscripts FOR j = 1 to 3 INPUT a(i,j) ‘input into array NEXT ‘using subscripts NEXT i = 2: j=1 PRINT a(i,j) ‘use any array element by specifying exact location

  23. Arrays • collection of storage locations • accessed by the same variable name • store the same kind of data - numeric or string • individual storage locations are called elements

  24. number of storage locations associated with a variable must be indicated - DIM statement • subscripts identify particular elements of an array • arrays may be one or two dimensional • to identify a particular element both the array name and the subscript must be specified • use array element as an ordinary variable as long as subscript is specified

  25. DIM statement 1. DIM variable ([lower TO] upper), .... for single dimension arrays e.g. DIM names$(7), area(2 TO 10) 2. DIM variable(row,column) for two dimensional arrays e.g. DIM num(2,3), DIM rate (2 to 11, 1 to 5)

  26. DIM day$(7) FOR I = 1 TO 7 READ day$(I) NEXT DATA Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday PRINT MID$(day$(3), 2, 3) k = 7 : PRINT day$(k) j = 1 : day$(j) = “cancelled” FOR X = 1 TO 7 PRINT day$(X) NEXT

  27. Problem • Using the first part of the previous program write some code which will display the days of the week (Monday to Sunday) and request the number of hours studied each day and calculate the average.

  28. Example • Investors are planning to build a ski resort. They have three possible sites under study. Naturally they are interested in the snowfall at these sites. They have collected data on the snowfall, in centimetres, for the three sites for the last 5 years as shown following:

  29. Data Year Site Num12345 1 106 147 139 153 126 2 143 151 117 134 139 3 136 143 130 142 145

  30. problem • Write a program that • takes the information from DATA statements and places it in a 2 dimensional array • calculates the average snowfall for the last 5 years for each of the three sites • prints out the data and the calculated average in a table • Calculates and prints the grand average snowfall for ALL three sites.

  31. The End

  32. FOR i = 1 to 2 PRINT “hello” NEXT • i = 1 (initial); i > final? • prints hello • i is incremented by STEP (default of 1) and loops back; i = 2; i > final? • prints hello • i is incremented; i= 3; tests; exits loop

  33. Example FOR i = 1 TO 10 STEP 2 FOR j = 20 TO 2 STEP -.02 FOR k = 2 TO 5 FOR i = X TO Y STEP a/b^2

  34. FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT Complete the table

  35. FOR I = 1 TO 3 FOR J = 1 TO 3 PRINT I + J NEXT NEXT Complete the table

  36. FOR NEXT Loops • used when know number of loop executions • used as element position in arrays • maybe nested • do not use EXIT FOR - > DO LOOP

  37. used for loops with a fixed or known number of iterations • low, high and increment can be variables, numbers, or expressions • i - controlvariable: controls the number of iterations in the loop • when the loop starts controlvariable = lowvariable • at loop end (NEXT) contolvariable = controlvariable + increment

  38. loop executes if controlvariable <= highvalue • loop does not execute if controlvariable is greater then high value • number of loop iterations; • (highvalue - lowvalue + 1)/increment • default increment is 1and cannot =0 • increment may be positive or negative • STEP values must be a consistent data type with controlvariable

More Related