1 / 35

Chapter 5: Looping

Chapter 5: Looping. Using the while Loop. Loop A structure that allows repeated execution of a block of statements Loop body A block of statements within a looping structure C# types of loops: while loop for loop do loop (or do...while loop). Using the while Loop (cont’d.).

gella
Download Presentation

Chapter 5: Looping

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. Chapter 5: Looping

  2. Using the while Loop • Loop • A structure that allows repeated execution of a block of statements • Loop body • A block of statements within a looping structure • C# types of loops: • while loop • for loop • do loop (or do...while loop) Microsoft Visual C# 2012, Fifth Edition

  3. Using the whileLoop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  4. Using the while Loop (cont’d.) • while loop • Used to execute a body of statements continuously as long as some condition continues to be true • Infinite loop • A loop that never ends • Making a while loop end correctly • Initialize the loop control variable • Test the control variable in the while expression • Alter the value of the control variable in the code block Microsoft Visual C# 2012, Fifth Edition

  5. Using the while Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  6. Using the while Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  7. Using the while Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  8. Using the while Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  9. Using the while Loop (cont’d.) • Empty body • A body with no statements in it • Alter the control variable by: • Incrementing, or adding to it • Decrementing, or subtracting from it • Definite loop or counted loop • A loop for which the number of iterations is predetermined • Indefinite loop • The value of a loop control variable is not altered by arithmetic, but instead is altered by user input Microsoft Visual C# 2012, Fifth Edition

  10. Using the while Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  11. Using the for Loop • for loop • A shorthand way to create definite loops • Sections of the loop • Control variable initialization • Control variable testing • Control variable updating Microsoft Visual C# 2012, Fifth Edition

  12. Using the for Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  13. Using the do Loop • do loop • Checks at the “bottom” of the loop after one repetition has occurred • Convenient when you know you want to perform some task at least one time Microsoft Visual C# 2012, Fifth Edition

  14. Using the do Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  15. Using the do Loop (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  16. Microsoft Visual C# 2012, Fifth Edition

  17. Using Nested Loops • When loops are nested, each pair contains an inner loop and an outer loop • The inner loop must be entirely contained within the outer loop • Loops can never overlap Microsoft Visual C# 2012, Fifth Edition

  18. Using Nested Loops (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  19. Microsoft Visual C# 2012, Fifth Edition

  20. Microsoft Visual C# 2012, Fifth Edition

  21. Accumulating Totals • Totals are accumulated • Gathered together and added into a final sum by processing individual records one at a time in a loop Microsoft Visual C# 2012, Fifth Edition

  22. Accumulating Totals (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  23. Accumulating Totals (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  24. Improving Loop Performance • Make sure the loop does not include unnecessary operations or statements • Example: A loop should execute while x is less than the sum of two integers, a and b • Initial solution while(x < a + b) // loop body • Better solution int sum = a + b; while(x < sum) // loop body Microsoft Visual C# 2012, Fifth Edition

  25. Improving Loop Performance (cont’d.) Microsoft Visual C# 2012, Fifth Edition • Comparing to zero • Making a comparison to 0 is faster than making a comparison to any other value

  26. Improving Loop Performance (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  27. Improving Loop Performance (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  28. Improving Loop Performance (cont’d.) Microsoft Visual C# 2012, Fifth Edition • Using prefix incrementing rather than postfix incrementing • A common operation in forloops

  29. Improving Loop Performance (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  30. Improving Loop Performance (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  31. Looping Issues in GUI Programs • Using a loop within a method in a GUI application is no different from using one in a console application • Event-driven programs sometimes require fewer coded loops • Some events are determined by the user’s actions when the program is running, rather than by the programmer’s coding Microsoft Visual C# 2012, Fifth Edition

  32. Looping Issues in GUI Programs (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  33. Looping Issues in GUI Programs (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  34. Looping Issues in GUI Programs (cont’d.) Microsoft Visual C# 2012, Fifth Edition

  35. Looping Issues in GUI Programs (cont’d.) Microsoft Visual C# 2012, Fifth Edition

More Related