Microsoft Visual C# 2010       Fourth Edition        Chapter 5         Looping
Objectives• Learn how to create loops using the while  statement• Learn how to create loops using the for statement• Learn...
Using the while Loop• Loop    – Structure that allows repeated execution of a block of      statements• Loop body    – Blo...
Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition   4
Using the while Loop (contd.)• while loop    – Used to execute a body of statements continuously as      long as some cond...
Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition   6
Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition   7
Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition   8
Using the while Loop (contd.)• Empty body    – Body with no statements in it• Alter the control variable by:    – Incremen...
Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition   10
Using the for Loop• for loop    – Shorthand way to create definite loops• Sections of the loop    – Control variable initi...
Using the for Loop (contd.)• Other tasks (contd.)    – Decrement or perform some other task at the end of      the loop’s ...
Using the for Loop (contd.)Microsoft Visual C# 2010, Fourth Edition    13
Using the do Loop• do loop    – Checks at the bottom of the loop after one repetition      has occurred• Convenient when y...
Using the do Loop (contd.)Microsoft Visual C# 2010, Fourth Edition    15
Using the do Loop (contd.)Microsoft Visual C# 2010, Fourth Edition    16
Using Nested Loops• When loops are nested, each pair contains an inner  loop and an outer loop    – The inner loop must be...
Using Nested Loops (contd.)Microsoft Visual C# 2010, Fourth Edition   18
Microsoft Visual C# 2010, Fourth Edition   19
Accumulating Totals• Totals are accumulated    – Gathered together and added into a final sum          • By processing ind...
Accumulating Totals (contd.)Microsoft Visual C# 2010, Fourth Edition    21
Accumulating Totals (contd.)Microsoft Visual C# 2010, Fourth Edition    22
Improving Loop Performance• Make sure the loop does not include unnecessary  operations or statements• Example    – A loop...
Looping Issues in GUI Programs• Using a loop within a method in a GUI application is  no different from using one in a con...
Looping Issues in GUI Programs                    (contd.)Microsoft Visual C# 2010, Fourth Edition   25
Looping Issues in GUI Programs                    (contd.)Microsoft Visual C# 2010, Fourth Edition   26
You Do It• Activities to explore   – Using a while Loop   – Using for Loops Microsoft Visual C# 2010, Fourth Edition      ...
Summary• A loop is a structure that allows repeated execution  of a block of statements• Use a while loop to execute a bod...
Summary (contd.)• The do loop checks the bottom of the loop after  one repetition has occurred• You can nest any combinati...
Upcoming SlideShare
Loading in …5
×

Csc153 chapter 05

614 views

Published on

Published in: Technology, Spiritual
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
614
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
49
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Csc153 chapter 05

  1. 1. Microsoft Visual C# 2010 Fourth Edition Chapter 5 Looping
  2. 2. Objectives• Learn how to create loops using the while statement• Learn how to create loops using the for statement• Learn how to create loops using the do statement• Use nested loops• Accumulate totals• Understand how to improve loop performance• Learn about looping issues in GUI programsMicrosoft Visual C# 2010, Fourth Edition 2
  3. 3. Using the while Loop• Loop – Structure that allows repeated execution of a block of statements• Loop body – Block of statements within a looping structure• C# types of loops – while loop – for loop – do loop (or do-while loop)Microsoft Visual C# 2010, Fourth Edition 3
  4. 4. Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 4
  5. 5. Using the while Loop (contd.)• 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 variableMicrosoft Visual C# 2010, Fourth Edition 5
  6. 6. Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 6
  7. 7. Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 7
  8. 8. Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 8
  9. 9. Using the while Loop (contd.)• Empty body – Body with no statements in it• Alter the control variable by: – Incrementing, or adding to it – Decrementing it• Definite loop or counted loop – Loop for which the number of iterations is predetermined• Indefinite loop – Value of a loop control variable is not altered by arithmetic, but instead, is altered by user inputMicrosoft Visual C# 2010, Fourth Edition 9
  10. 10. Using the while Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 10
  11. 11. Using the for Loop• for loop – Shorthand way to create definite loops• Sections of the loop – Control variable initialization – Control variable testing – Control variable updating• Other tasks – Initialize more than one variable – Declare a new variable – Perform more than one testMicrosoft Visual C# 2010, Fourth Edition 11
  12. 12. Using the for Loop (contd.)• Other tasks (contd.) – Decrement or perform some other task at the end of the loop’s execution – Perform multiple tasks at the end of the loop’s execution – Leave one or more portions of the for expression emptyMicrosoft Visual C# 2010, Fourth Edition 12
  13. 13. Using the for Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 13
  14. 14. 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 timeMicrosoft Visual C# 2010, Fourth Edition 14
  15. 15. Using the do Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 15
  16. 16. Using the do Loop (contd.)Microsoft Visual C# 2010, Fourth Edition 16
  17. 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 overlapMicrosoft Visual C# 2010, Fourth Edition 17
  18. 18. Using Nested Loops (contd.)Microsoft Visual C# 2010, Fourth Edition 18
  19. 19. Microsoft Visual C# 2010, Fourth Edition 19
  20. 20. Accumulating Totals• Totals are accumulated – Gathered together and added into a final sum • By processing individual records one at a time in a loop• Garbage – Unknown value – C# compiler helps to prevent seeing an incorrect total • By requiring you to provide a starting value – C# will not use the garbage value that happens to be stored at an uninitialized memory locationMicrosoft Visual C# 2010, Fourth Edition 20
  21. 21. Accumulating Totals (contd.)Microsoft Visual C# 2010, Fourth Edition 21
  22. 22. Accumulating Totals (contd.)Microsoft Visual C# 2010, Fourth Edition 22
  23. 23. 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 bodyMicrosoft Visual C# 2010, Fourth Edition 23
  24. 24. 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 – You can write an event-driven program so that an action continues as long as the user continues to make an appropriate selectionMicrosoft Visual C# 2010, Fourth Edition 24
  25. 25. Looping Issues in GUI Programs (contd.)Microsoft Visual C# 2010, Fourth Edition 25
  26. 26. Looping Issues in GUI Programs (contd.)Microsoft Visual C# 2010, Fourth Edition 26
  27. 27. You Do It• Activities to explore – Using a while Loop – Using for Loops Microsoft Visual C# 2010, Fourth Edition 27
  28. 28. Summary• A loop is a structure that allows repeated execution of a block of statements• Use a while loop to execute a body of statements continuously while some condition continues to be true• When using a for statement, you can indicate in one place: – Starting value for the loop control variable – Test condition that controls loop entry – Expression that alters the loop control variableMicrosoft Visual C# 2010, Fourth Edition 28
  29. 29. Summary (contd.)• The do loop checks the bottom of the loop after one repetition has occurred• You can nest any combination of loops to achieve desired results• In computer programs, totals frequently are accumulated• Improve loop performance by making sure loop does not include unnecessary operations• You can use while, for, and do...while loops in the same ways in console and GUI programsMicrosoft Visual C# 2010, Fourth Edition 29

×