Loops

11,010 views

Published on

pre testing and post testing loops C++

1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
11,010
On SlideShare
0
From Embeds
0
Number of Embeds
80
Actions
Shares
0
Downloads
332
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Loops

  1. 1. Repetition (Looping Concept)
  2. 2. After studying this chapter you will be able to: O BJECTIVES  Understand the basic components of a loop: initialization, control expression, and update.  Understand and use pretest, post-test, and count-controlled loops.  Differentiate between event-controlled and counter-controlled loops.  Write loops in C++ using while , for , and do...while loops.  Understand the limitations and use of break and continue statements in loops.
  3. 3. Concept of a Loop
  4. 4. <ul><li>The two most important features of a computer are: </li></ul><ul><ul><li>The ability to compare values to make decisions, and </li></ul></ul><ul><ul><li>The ability to repeat a series of operations in a controlled way. </li></ul></ul>
  5. 5. Figure : The concept of a loop
  6. 6. Pretest and Post-Test Loops
  7. 7. <ul><li>Loops need to be coded in such a way as that they may be ended in a controlled manner. </li></ul><ul><li>The decision making process make take place at either the ‘ top ’ of the loop or at the ‘ bottom ’. </li></ul><ul><li>When the test takes place at the top of the loop, it is referred to as a ‘Pretest Loop’. </li></ul><ul><li>When the test takes place at the bottom of the loop, it is then a ‘Post-test Loop’. </li></ul>
  8. 8. Pretest Loop In each iteration, the loop control expression is tested first. If it is true, the loop action(s) is executed; if it is false, the loop is terminated. Post-test Loop In each iteration, the loop action(s) are executed. Next, the loop control expression is tested. If it is true, a new iteration is started; otherwise, the loop terminates. Note:
  9. 9. Figure : Pretest and post-test loops “ WHILE” test at the top…. “WHILE” test at the bottom CONDITION CONDITION
  10. 10. Figure : Two different strategies for starting exercise CONDITION CONDITION False True
  11. 11. Figure : Minimum number of iterations in two loops It is important that you understand this critical difference between the two basic types…
  12. 12. Initialization and Updating
  13. 13. Figure : Loop initialization and updating The initialization may optionally be part of the loop structure.
  14. 14. Figure : Initialization and updating for exercise
  15. 15. Event-Controlled and Counter-Controlled Loops
  16. 16. Figure : Event-controlled loop concept
  17. 17. Figure : Counter-controlled loop concept
  18. 18. Loops in C++
  19. 19. Figure : C++ loop constructs <ul><li>All three C/C++ loop constructs involve a test. </li></ul><ul><li>The only difference is when the test occurs – either before or after doing something.. </li></ul>
  20. 20. Figure : The while statement while the expression is true, do …..
  21. 21. Figure : Compound while statement
  22. 22. Figure : for statement The for is just a special case of a while loop.
  23. 23. The for loop is constructed in this way… for ( initialization statement(s) , while test expression , update statement(s) )‏ { body of loop… }
  24. 24. A for loop is used when your loop is to be executed a known number of times. You can do the same thing with a while loop, but the for loop is easier to read and more natural for counting loops. Note:
  25. 25. Figure : Compound for statement
  26. 26. Figure : Comparing for and while loops
  27. 27. In C/C++ the for loop counter is almost always initialized to a ZERO . So, if you want to do something 5 times, the for loop would be coded like this… for (int i = 0; i < 5; i++)‏ { do something… } It is also legal to have the test expression limit be a variable, as in: for (int i = 0; i < Num; i++)‏
  28. 28. Figure : Format of the do…while statement
  29. 29. Pre-test and post-test loops
  30. 30. Assignment <ul><li>Write a program (WAP) in C++ to accept a number and print its table upto 10 terms. </li></ul><ul><li>WAP in C++ to print the following output using for loops: </li></ul><ul><li>1 </li></ul><ul><li>22 </li></ul><ul><li>333 </li></ul><ul><li>4444 </li></ul><ul><li>WAP in C++ to accept a number and calculate its factorial. </li></ul><ul><li>eg. If number = 5 </li></ul><ul><li>Factorial of 5 = 120 should be displayed </li></ul>
  31. 31. The End

×