csi 101 elements of computing spring 2009 lecture # 8 looping and recursion wednesday, february 25...

24
CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th , 2009

Post on 20-Dec-2015

218 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

CSI 101 Elements of Computing

Spring 2009

Lecture # 8Looping and Recursion

Wednesday, February 25th, 2009

Page 2: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Straight-line processing

● Process flows in straight line

● Could divide, but comes back together

● Never comes back to earlier point

● In a perfect world, all processes would be straight-line

● Bad news – the world isn't perfect

Page 3: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Looping

● Process of “encasing” statements in a loop● In other words, flow goes backwards to

earlier point and reruns statements● Same loop always contains same statements

● Exact flow might change based upon conditions● For instance:

IF Count < 3 THEN:

ELSE:

ENDIF

Page 4: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Loops in Pseudocode

● We already saw two of these:● DO WHILE● DO ... UNTIL

● The loop “body” were the statements between DO and END

● Uses some True or False condition to determine when we end the looping

● Next statement after loop is statement following the ENDDO or ENDWHILE

Page 5: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Visual Basic Form

● Do While or Do Until begins loop● Note condition begins loop no matter which type

you use● Do Until still loops once before checking

condition● Loop statement ends loop● Example:

DO While (intCount < 10):

Loop

Page 6: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Counting Loops

● What if we know how many times we need to loop?

● Could use a counter, and increment the counter each time through the loop● Our exit condition is testing the counter for less

(on WHILE) or equal (on UNTIL) the end value● For instance:

Count = 1DO WHILE (Count < 10)

:Count = Count + 1

Loop

Page 7: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

FOR loops

● New statement : FOR● Used for counting loops● Syntax:

FOR Count = 1 TO 10

:

NEXT Count

● Loop starts at FOR statement● Loop ends with NEXT statement

Page 8: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

How do FOR loops work?

FOR Count = 1 TO 10:

NEXT Count● Hit FOR 1st time : set Count to start value

● In this case, Count = 1● Process loop body● Hit NEXT : increment Count● Go back to FOR statement● Test value of Count against end value (10)● Loop again if value is less or equal to end

value

Page 9: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

FOR example

Sum = 0FOR Count = 1 TO 10

Sum = Sum + CountNEXT Count

● What is the end value of Sum?● 1st pass: Count = 1, Sum = 1● 2nd pass: Count = 2, Sum = 3● 3rd pass: Count = 3, Sum = 6● 4th pass: Count = 4, Sum = 10● 5th pass: Count = 5, Sum = 15● 6th pass: Count = 6, Sum = 21

Page 10: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

FOR example, cont

Sum = 0FOR Count = 1 TO 10

Sum = Sum + CountNEXT Count● 7th pass: Count = 7, Sum = 28● 8th pass: Count = 8, Sum = 36● 9th pass: Count = 9, Sum = 45● 10th pass: Count = 10, Sum = 55● End of loop: Count =11, Sum = 55● Why is Count = 11?

Page 11: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

STEP option

● Sometimes, especially if loop variable is used in calculation in loop body, we don't want to increment by 1

● Use STEP option:FOR Count = 1 TO 10 STEP 2● This adds 2 to Count at each NEXT statement

Page 12: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Revised FOR example

Sum = 0FOR Count = 1 TO 10 STEP 2

Sum = Sum + CountNEXT Count

● 1st pass : Count = 1, Sum = 1● 2nd pass : Count = 3, Sum = 4● 3rd pass : Count = 5, Sum = 9● 4th pass : Count = 7, Sum = 16● 5th pass : Count = 9, Sum = 25● Loop ends : Count = 11, Sum = 25

Page 13: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

How many passes?

● To double-check your FOR loop construction, you can walk through it

● If you just need to know how many it processes, use formula:

(End value – start value + 1)/(STEP value)●drop any fraction

Page 14: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Careful with your STEPs

● Watch what you write!● If you accidentally used STEP -2, how many

times would loop run?● Only once

● Can use negative steps● Then have end value smaller than start value● For instance:

FOR Count = 10 TO 1 STEP -1● Now NEXT statement decrements and loops if

Count is greater than or equal to end value

Page 15: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

FORs for Arrays

● Use FOR EACH loop to loop through each element in an array

● No start or end values specified● Specify variable of same data type as array

● Example:DIM intValues(20) As IntegerDIM index As IntegerFOR Each index IN intValues

:Next index

Page 16: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Nested loops

● Loops within loops● Not recommended for same variable, as

could screw up flows● Ideal for certain situations● Example: 2-dimensional table

● Perform action on all cells of tableFOR Row = 1 TO 12

FOR Column = 1 TO 10:

NEXT ColumnNEXT Row

Page 17: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Nested Loops, cont

● Nested loops don't have to be same type

● Could nest a DO within a FOR, or vice versa

● Basically, each pass of outer loop will cause inner loop to run its required number of times

Page 18: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Nested Loop Example

● We have twelve bags of marbles. Counting number of red, blue, and white marbles in total among the bags: DIM totalRed As Integer =0, totalBlue As Integer = 0

DIM totalWhite As Integer =0 Structure Marble

Color as Integer ‘Red is 1, blue is 2, white is 3 End Structure DIM Marbles(120) As Marble ‘entire collection of marbles Dim Bag(20) As Integer ‘fit up to 20 marbles in bag

‘value in array is index into Marbles array Dim Bags(12) As Integer Dim bagIndex As Integer, index as Integer

Page 19: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Nested Loop Example, cont

FOR bagIndex = 1 TO 12For Each index in Bags(bagIndex)

SELECT CASE Marbles(index).ColorCASE 1

totalRed += 1CASE 2

totalBlue += 1CASE 3

totalWhite += 1Next index

NEXT bagIndex

Page 20: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Recursion

● A function, routine, or block of code that calls itself

● Different form of looping● Allows passing of parameters and use of

local variables● Can stop looping based upon calculated

values that change OUTSIDE routine

Page 21: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Why use recursion?

● Allows for parallel processing● Example: Database of University students

● In database applications, can prevent

potential deadlock● For mathematical calculations, can handle

formulas that rely on previous values● Sequences and series

Page 22: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Recursion Example

● Fibonacci sequenceBEGIN Sequence

INPUT Term1, Term2PRINT Term1, Term2CALL Fibonacci(Term1, Term2)

END Sequence

BEGIN Fibonacci(T1, T2)PRINT T1 + T2CALL Fibonacci(T2, T1 + T2)

END Fibonacci

Page 23: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

Refining the Sequence

● Fibonacci is an infinite sequence● Our example never ends● Need to receive a value of how many terms

to print

BEGIN Sequence2INPUT NumTerms, Term1, Term2Print Term1, Term2CALL Fib2(NumTerms, Term1, Term2)

END Sequence2

Page 24: CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009

New Sequence Example

BEGIN Fib2(n, T1, T2)PRINT T1 + T2n = n – 1IF n = 0 THEN GOTO DoneCALL Fib2(n, T2, T1 + T2)

Done: END Fib2

• Why did I place the label at the END statement?