Chapter 5: Looping · Looping Issues in GUI Programs •Using a loop within a method in a GUI...

27
Chapter 5: Looping

Transcript of Chapter 5: Looping · Looping Issues in GUI Programs •Using a loop within a method in a GUI...

Page 1: Chapter 5: Looping · 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

Chapter 5:

Looping

Page 2: Chapter 5: Looping · 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

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)

2 Microsoft Visual C# 2012, Fifth Edition

Page 3: Chapter 5: Looping · 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

Using the while Loop (cont’d.)

3 Microsoft Visual C# 2012, Fifth Edition

Page 4: Chapter 5: Looping · 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

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

4 Microsoft Visual C# 2012, Fifth Edition

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

Using the while Loop (cont’d.)

5 Microsoft Visual C# 2012, Fifth Edition

Page 6: Chapter 5: Looping · 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

Using the while Loop (cont’d.)

6 Microsoft Visual C# 2012, Fifth Edition

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

Using the while Loop (cont’d.)

7 Microsoft Visual C# 2012, Fifth Edition

Page 8: Chapter 5: Looping · 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

8 Microsoft Visual C# 2012, Fifth Edition

Using the while Loop (cont’d.)

Page 9: Chapter 5: Looping · 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

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

9 Microsoft Visual C# 2012, Fifth Edition

Page 10: Chapter 5: Looping · 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

Using the while Loop (cont’d.)

10 Microsoft Visual C# 2012, Fifth Edition

Page 11: Chapter 5: Looping · 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

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

11 Microsoft Visual C# 2012, Fifth Edition

Page 12: Chapter 5: Looping · 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

Using the for Loop (cont’d.)

12 Microsoft Visual C# 2012, Fifth Edition

Page 13: Chapter 5: Looping · 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

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

13 Microsoft Visual C# 2012, Fifth Edition

Page 14: Chapter 5: Looping · 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

14 Microsoft Visual C# 2012, Fifth Edition

Using the do Loop (cont’d.)

Page 15: Chapter 5: Looping · 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

15 Microsoft Visual C# 2012, Fifth Edition

Page 16: Chapter 5: Looping · 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

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

16 Microsoft Visual C# 2012, Fifth Edition

Page 17: Chapter 5: Looping · 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

Using Nested Loops (cont’d.)

17 Microsoft Visual C# 2012, Fifth Edition

Page 18: Chapter 5: Looping · 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

18 Microsoft Visual C# 2012, Fifth Edition

Page 19: Chapter 5: Looping · 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

19 Microsoft Visual C# 2012, Fifth Edition

Page 20: Chapter 5: Looping · 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

Accumulating Totals

• Totals are accumulated

– Gathered together and added into a final sum by processing individual records one at a time in a loop

20 Microsoft Visual C# 2012, Fifth Edition

Page 21: Chapter 5: Looping · 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

Accumulating Totals (cont’d.)

21 Microsoft Visual C# 2012, Fifth Edition

Page 22: Chapter 5: Looping · 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

Accumulating Totals (cont’d.)

22 Microsoft Visual C# 2012, Fifth Edition

Page 23: Chapter 5: Looping · 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

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

23 Microsoft Visual C# 2012, Fifth Edition

Page 24: Chapter 5: Looping · 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

24 Microsoft Visual C# 2012, Fifth Edition

Looping Issues in GUI Programs (cont’d.)

Page 25: Chapter 5: Looping · 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

25 Microsoft Visual C# 2012, Fifth Edition

Looping Issues in GUI Programs (cont’d.)

Page 26: Chapter 5: Looping · 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

26 Microsoft Visual C# 2012, Fifth Edition

Looping Issues in GUI Programs (cont’d.)

Page 27: Chapter 5: Looping · 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

27 Microsoft Visual C# 2012, Fifth Edition

Looping Issues in GUI Programs (cont’d.)