1 / 19

Introduction to Programming

Introduction to Programming. Loops. 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

akiva
Download Presentation

Introduction to Programming

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. Introduction to Programming Loops

  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

  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 iCnt = 0; while (iCnt < iSize) { //Do something iCnt = iCnt + 1;} Wait! This isn’t a For loop? iCnt < iSize F T Do something

  9. For Loop for (iCnt=0;iCnt <=4;iCnt++) { //Do something } Ahhhhhh! That’s better! iCnt < 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. Repeat loop • A repeat loop is a control structure where the body is repeated until the condition is true Body Condition F T

  12. Repeat loop • When the condition is true, the body is bypassed, and flow continues with the next part of the algorithm Body Condition F T

  13. Example: Adding Sequences iCnt = 0; iTot = 0; do { iCnt = iCnt + 1; iTot = iTot + iCnt; } while (iCnt != iSize); Body Condition F T

  14. Recursivecall Recursion • When a subroutine or function calls itself private int sumIt(int N) { int iTotal; if (N > 0) sumIt = sumIt(N – 1) + N; else sumIt = N; }

  15. 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)

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

  17. Recursion • Each time you make a recursive call, the state of the calling routine is saved on the program stack (part of memory) • But … the stack is only so big • You can run out of space!

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

  19. Try this! • Look up Fibonacci numbers and create a function (method) to generate the nth number

More Related