1 / 15

Loops

Loops. Website Production. Q: What is a Loop?. A control structure that allows for a sequence of steps to be repeated a certain number of times This sequence of steps is called the body of the loop. Q: What is a Loop?. There are three basic loop structures in programming: For While

banyan
Download Presentation

Loops

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. Loops Website Production

  2. Q: What is a Loop? • A control structure that allows for a sequence of steps to be repeated a certain number of times • This sequence of steps is called the body of the loop

  3. Q: What is a Loop? • There are three basic loop structures in programming: • For • While • Repeat (not in Javascript)

  4. While loop • A while loop is a control structure where the body is repeated as long as the condition is true Condition F T Body

  5. While loop • When the condition is false, the body is bypassed, and flow continues with the next part of the algorithm Condition F T Body

  6. Example: Adding Sequences k  0 total  0 while (k<size) k  k + 1 total  total + k (k<size) F T k  k + 1 total  total + k

  7. Example: Adding Sequences iCnt = 0; iTot = 0; while (iCnt<iSize){ iCnt = iCnt + 1; iTot = iTot + iCnt; } iCnt < iSize F T iCnt = iCnt + 1 iTot = iTot + iCnt

  8. For Loop i = 0; while (i < iSize){ //Do something i = i + 1;} Wait! This isn’t a For loop? iCnt < iSize F T Do something

  9. Initialize counter Test for end condition Increment counter For Loop for (i=0; i<iSize; i++){ //Do something} Ahhhhhh! That’s better! i < iSize F T Do something

  10. Exercise • The function factorial (n!) is defined as the product of the integers 1 through n. • Create two functions to compute n!, the first version using a for loop and the second using a while loop.

  11. Recursivecall Recursion • When a subroutine or function calls itself function sumIt(N){ if (N > 0) out = sumIt(N – 1) + N; else out = N; return out; }

  12. sumIt  4 + 6 sumIt(3) sumIt  3 + 3 sumIt(2) sumIt  2 + 1 sumIt(1) sumIt  1 + 0 sumIt(0) sumIt  0 Recursion Example: sumIt(4)

  13. Recursion • But … there’s only so far down we can go • Question : How many recursive calls would it take to evaluate sumIt(10000)?

  14. Recursion • Each time you make a recursive call, the state of the calling routine is saved in memory • But …You can run out of space!

  15. Recursion • A better solution is to re-write the routine using a loop instead of recursion • Any ideas?

More Related