Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009."— Presentation transcript:

1

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

3 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

4 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

5 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

6 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

7 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 = 1 DO WHILE (Count < 10) : Count = Count + 1 Loop

8 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

9 How do FOR loops work? FOR Count = 1 TO 10 : NEXT Count ● Hit FOR 1 st 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

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

11 FOR example, cont Sum = 0 FOR Count = 1 TO 10 Sum = Sum + Count NEXT Count ● 7 th pass: Count = 7, Sum = 28 ● 8 th pass: Count = 8, Sum = 36 ● 9 th pass: Count = 9, Sum = 45 ● 10 th pass: Count = 10, Sum = 55 ● End of loop: Count =11, Sum = 55 ● Why is Count = 11?

12 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

13 Revised FOR example Sum = 0 FOR Count = 1 TO 10 STEP 2 Sum = Sum + Count NEXT Count ● 1 st pass : Count = 1, Sum = 1 ● 2 nd pass : Count = 3, Sum = 4 ● 3 rd pass : Count = 5, Sum = 9 ● 4 th pass : Count = 7, Sum = 16 ● 5 th pass : Count = 9, Sum = 25 ● Loop ends : Count = 11, Sum = 25

14 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

15 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

16 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 Integer DIM index As Integer FOR Each index IN intValues : Next index

17 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 table FOR Row = 1 TO 12 FOR Column = 1 TO 10 : NEXT Column NEXT Row

18 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

19 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

20 Nested Loop Example, cont FOR bagIndex = 1 TO 12 For Each index in Bags(bagIndex) SELECT CASE Marbles(index).Color CASE 1 totalRed += 1 CASE 2 totalBlue += 1 CASE 3 totalWhite += 1 Next index NEXT bagIndex

21 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

22 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

23 Recursion Example ● Fibonacci sequence BEGIN Sequence INPUT Term1, Term2 PRINT Term1, Term2 CALL Fibonacci(Term1, Term2) END Sequence BEGIN Fibonacci(T1, T2) PRINT T1 + T2 CALL Fibonacci(T2, T1 + T2) END Fibonacci

24 Refining the Sequence ● Fibonacci is an infinite sequence ● Our example never ends ● Need to receive a value of how many terms to print BEGIN Sequence2 INPUT NumTerms, Term1, Term2 Print Term1, Term2 CALL Fib2(NumTerms, Term1, Term2) END Sequence2

25 New Sequence Example BEGIN Fib2(n, T1, T2) PRINT T1 + T2 n = n – 1 IF n = 0 THEN GOTO Done CALL Fib2(n, T2, T1 + T2) Done: END Fib2 Why did I place the label at the END statement?


Download ppt "CSI 101 Elements of Computing Spring 2009 Lecture # 8 Looping and Recursion Wednesday, February 25 th, 2009."

Similar presentations


Ads by Google