Looping - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

Looping

Description:

'I will not pour Clorox in the fish tank' Have to write this sentence either ... Output: 1 I will not pour Clorox in the Fish Tank. counter. 1. Why this works ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 74
Provided by: jeffch8
Category:
Tags: clorox | looping

less

Transcript and Presenter's Notes

Title: Looping


1
Looping
  • Helping Unruly School Children Around the World

2
Overview
  • Iteration
  • 4 kinds of loops
  • for
  • while
  • do while
  • foreach
  • Infinite Loops

3
Iteration
  • One thing that computers do well is repeat
    commands
  • Programmers use loops to accomplish this
  • 4 kinds of loops in C
  • for loop
  • while loop
  • do while loop
  • foreach loop

4
Criteria for loops
  • Usually have some initial condition
  • Starting a counter
  • Beginning in a certain state
  • Must have a test to continue
  • Must make progress towards finishing

5
Loops in Everyday Life
  • Bad children are told to write sentences on the
    board
  • I will not pour Clorox in the fish tank
  • Have to write this sentence either
  • A certain number of times
  • Until the teacher is happy
  • As many as you can during break

6
The for loop
  • Good when you know exactly how many times you
    need to execute something
  • Has format
  • for (ltinitializationgt lttest to continuegt
    ltincrementgt)
  • // everything in here is what is repeated
  • // over and over again
  • Initialization is where the counter is given a
    starting value
  • The test determines whether or not to continue
  • The increment can be any amount, including
    negative, and occurs after the loop statements
    execute

7
Satisfying the Teacher
  • Example 1000 sentences? No problem
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (I will not pour Clorox)
  • // Remember, counter is the same as
  • // counter counter 1

8
But I want them numbered!
  • No problem
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

9
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

1
10
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

1
true
11
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)
  • Output 1 I will not pour Clorox in the Fish Tank

1
12
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

2
13
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

2
true
14
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)
  • Output 2 I will not pour Clorox in the Fish Tank

2
15
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

3
16
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

3
true
17
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)
  • Output 3 I will not pour Clorox in the Fish Tank

3
18
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

4
19
When will it end?
  • We see that this will go on for a while
  • Its a little more interesting later around 1000

20
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

999
true
21
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)
  • Output 999 I will not pour Clorox in the Fish
    Tank

999
22
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

1000
23
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

true for last time
1000
24
Why this works(are we finished?)
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)
  • Output 1000 I will not pour Clorox in the Fish
    Tank

1000
25
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)

1001
26
Why this works
counter
  • int counter
  • for (counter 1 counter lt 1000 counter)
  • Console.WriteLine (counter I will not
    pour)
  • // Jump down here and continue

false
1001
27
Final Output
  • 1 I will not pour Clorox in the fish tank.
  • 2 I will not pour Clorox in the fish tank.
  • 3 I will not pour Clorox in the fish tank.
  • 4 I will not pour Clorox in the fish tank.
  • .
  • .
  • .
  • 999 I will not pour Clorox in the fish tank.
  • 1000 I will not pour Clorox in the fish tank.

28
The while loop
  • Good for when you dont know how many times to
    repeat
  • Teacher says Write until Im happy
  • Has format
  • while (ltboolean valuegt)
  • // stuff to repeat over and over

29
Example
  • bool teacherHappy false
  • int lineNumber 1
  • while (!teacherHappy)
  • Console.WriteLine (lineNumber I will not)
  • lineNumber
  • teacherHappy attitudeFunction ( )
  • // assume attitudeFunction can change
  • // teacherHappy

30
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)

31
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)

32
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • Output 1 I will not pour Clorox in the fish tank

33
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)

34
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)

35
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • Output 1 I will not pour Clorox in the fish tank

36
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)

37
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)

38
Example Re-Writing 1-1000(using a while loop)
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • Output 1 I will not pour Clorox in the fish tank

39
Infinite Loops
  • This loop isnt making a lot of progress!
  • Loops that repeat forever are called infinite
    loops
  • Apparently lock up
  • Output
  • 1 I will not pour Clorox in the fish tank
  • 1 I will not pour Clorox in the fish tank
  • 1 I will not pour Clorox in the fish tank
  • 1 I will not pour Clorox in the fish tank
  • .
  • .
  • .


Continue forever
40
Problem Solved
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

41
Problem Solved
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

42
Problem Solved
counter
1
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter
  • Output 1 I will not pour Clorox in the fish tank

43
Problem Solved
counter
2
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

// Remember, counter is the same as// counter
counter 1
44
Example Re-Writing 1-1000(using a while loop)
counter
2
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

45
Problem Solved
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

2
46
Problem Solved
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter
  • Output 2 I will not pour Clorox in the fish tank

2
47
Problem Solved
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

3
48
How does it end?
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

999
49
How does it end?
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

999
50
How does it end?
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

999
51
Problem Solved
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter
  • Output 999 I will not pour Clorox in the fish
    tank

999
52
How does it end?
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

1000
53
How does it end?
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

1000
54
How does it end?
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter
  • // So we never print out
  • // 1000 I will not pour Clorox in the fishtank

now false
1000
55
Another Problem Solved
counter
  • int counter 1
  • while (counter lt 1000)
  • Console.WriteLine (counter I will not)
  • counter

now true
1000
56
The do-while loop
  • Similar to while loop
  • Must execute at least one time (test is at
    bottom)
  • Has format
  • do
  • while (ltboolean valuegt)

57
Example(count from 1 to 3)
counter
0
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

58
Example(count from 1 to 3)
counter
0
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

59
Example(count from 1 to 3)
counter
1
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

60
Example(count from 1 to 3)
counter
1
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

Output 1
61
Example(count from 1 to 3)
counter
1
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

62
Example(count from 1 to 3)
counter
2
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

63
Example(count from 1 to 3)
counter
2
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

Output 2
64
Example(count from 1 to 3)
counter
2
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

65
Example(count from 1 to 3)
counter
3
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)
  • // Note counter is now 3, but we still have
  • // to finish out the loop it doesnt skip

66
Example(count from 1 to 3)
counter
3
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

Output 3
67
Example(count from 1 to 3)
counter
3
  • int counter 0
  • do
  • counter
  • Console.WriteLine (counter)
  • while (counter lt 3)

now false, so loop is finished
68
The foreach loop
  • Often we have a collection of data
  • Wed like to do something to each one of the
    items in the collection
  • Certainly we can do this with a for loop
  • But C provides a new loop foreach
  • References an item in the collection
  • Provides access to it
  • Loops through all elements in the collection

69
foreach template
  • foreach (ltTYPEgt ltNAMEgt in ltCOLLECTIONgt)
  • WORK TO BE DONE ON EACH ITEM

70
A sample application
71
foreach example
  • Assuming we had a listbox control called
    listBoxNames with strings in it
  • foreach (string currentName in listBoxNames.Items)
  • MessageBox.Show(currentName.ToUpper())
  • Displays all of the strings in capitalization to
    the user in MessageBoxes.

72
foreach vs. for loop
  • The previous foreach loop on the collection using
    a for loop
  • for(int i0 i lt listBoxNames.Items.Count i)
  • string currentName (string)(listBoxNames.Items
    i)
  • MessageBox.Show(currentName.ToUpper())

73
Summary
  • for loops good for when you know how many times
    you want to repeat
  • while and do-while good for when you dont
  • foreach loop useful for processing collections
  • All loops must finish, or they become infinite
    loops
  • All loops must have a test to continue, or they
    become infinite loops
Write a Comment
User Comments (0)
About PowerShow.com