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

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
Pseudocode and Algorithms
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
CSI 101 Elements of Computing Spring 2009 Lecture #5 Designing with Pseudocode Wednesday, February 4th, 2009.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Programming Epson Robots – Part 2 ME 4135 – Fall 2012 Dr. R. Lindeke.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do While and Do Until Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
CPS120 Introduction to Computer Science Iteration (Looping)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Bordoloi and Bock Control Structures: Iterative Control.
Chapter 6 - VB.Net by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops Peek Method Counters and Accumulators Flags.
6.2 For…Next Loops General Form of a For…Next Loop
Developing Software Applications Iteration in Visual Basic (Loops)
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Chapter 7 Problem Solving with Loops
For…Next and Do...While Loops! Happy St. Patrick’s Day!
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Fortran: Control Structures Session Three ICoCSIS.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 – Repetition 5.1 Do Loops 5.2 Processing Lists of Data with Do Loops 5.3 For...Next Loops.
Chapter 6 - VB 2008 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study: Analyze a Loan.
UNIT 5 Lesson 15 Looping.
Loops BIS1523 – Lecture 10.
CS1371 Introduction to Computing for Engineers
Algorithm Analysis CSE 2011 Winter September 2018.
CISC101 Reminders Quiz 1 grading underway Next Quiz, next week.
Control Structure Senior Lecturer
Iteration: Beyond the Basic PERFORM
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Computing Fundamentals
Chapter 6 - VB.Net by Schneider
The structure of programming
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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

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

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

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

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

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

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

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

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?

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

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

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

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

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

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

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

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

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

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

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

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

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

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?