Download presentation
Presentation is loading. Please wait.
1
Welcome back to Software Development!
2
Loops
3
Loops 3 types of loops:
4
Loops 3 types of loops: while
5
Loops 3 types of loops: while
used when you don’t know how many times to loop
6
Loops 3 types of loops: while for
used when you don’t know how many times to loop for
7
Loops 3 types of loops: while for
used when you don’t know how many times to loop for used when you do know how many times to loop
8
Loops 3 types of loops: while for foreach
used when you don’t know how many times to loop for used when you do know how many times to loop foreach
9
Loops 3 types of loops: while for foreach
used when you don’t know how many times to loop for used when you do know how many times to loop foreach used to work on parts of a group, one at a time
10
All Loops…
11
All Loops… Use a loop control variable
12
All Loops… Use a loop control variable
First: Initialize the loop control variable
13
All Loops… Use a loop control variable
First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop
14
All Loops… Use a loop control variable
First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping
15
All Loops… Use a loop control variable
First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping Finally: Change (often increment) the loop control variable
16
All Loops… Use a loop control variable
First: Initialize the loop control variable If you don’t, you likely won’t be able to even get into the loop Next: Check the loop control variable to see if keep looping Finally: Change (often increment) the loop control variable If you don’t, you won’t be able to exit the loop…infinite loop
17
While Loop
18
While Loop Used when you don’t know how many times you need to loop
19
While Loop Used when you don’t know how many times you need to loop
Init the loop control variable outside and before the loop
20
While Loop Used when you don’t know how many times you need to loop
Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition
21
While Loop Used when you don’t know how many times you need to loop
Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop
22
While Loop Used when you don’t know how many times you need to loop
Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop Often increment the loop control variable when simply counting
23
While Loop while ( check loop control variable)
Used when you don’t know how many times you need to loop Init the loop control variable outside and before the loop Check the loop control variable in the while statement condition Change the loop control variable in the body of the while loop Often increment the loop control variable when simply counting while ( check loop control variable)
24
While Loop Example ArrayList myList = new ArrayList(); // create the list myList.Add(4); // fill the list int position; // create loop control variable position = 0; // init loop control variable while (position < myList.Count ) // loop until thru the list { Console.WriteLine(“Next #: ”, myList[position]); position++; // increment counter }
25
Task Write a program using a while loop to print the first 5 integers
Name the project “loops” Display each number on its own line with “Next #: Before the loop, display the message “While loop”
26
For Loop
27
For Loop Used when you *do* know how many times you need to loop
28
For Loop Used when you *do* know how many times you need to loop for (
29
For Loop for ( init the loop control variable ;
Used when you *do* know how many times you need to loop for ( init the loop control variable ;
30
For Loop for ( init ; check the loop control variable ;
Used when you *do* know how many times you need to loop for ( init ; check the loop control variable ;
31
For Loop for ( init ; check ; change the loop control variable)
Used when you *do* know how many times you need to loop for ( init ; check ; change the loop control variable)
32
For Loop for ( init ; check ; change )
Used when you *do* know how many times you need to loop for ( init ; check ; change )
33
For Loop
34
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable Increment loop control variable
35
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list
36
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable
37
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number
38
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( { Console.WriteLine(“Looped” + counter + “ times.”); }
39
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; { Console.WriteLine(“Looped” + counter + “ times.”); }
40
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable
41
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable
42
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable
43
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable
44
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); } Init loop control variable Check loop control variable Increment loop control variable
45
For Loop ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list int position; // create loop control variable // Loop until thru the list printing each number for ( position= 0 ; position < myList.Count ; position++ ) { Console.WriteLine(“Looped” + counter + “ times.”); }
46
Task Add to your “loops” program: After your while loop…
Add a for loop to print the first 5 integers Display each number on its own line with “Next #: Before the loop, display the message “For loop”
47
Foreach Loop
48
Foreach Loop Used to work on parts of a group, one at a time
49
Foreach Loop Used to work on parts of a group, one at a time
The loop control variable is created in the foreach statement
50
Foreach Loop Used to work on parts of a group, one at a time
The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable
51
Foreach Loop foreach ( Used to work on parts of a group, one at a time
The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach (
52
Foreach Loop foreach ( create loop control variable
Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable
53
Foreach Loop foreach ( create loop control variable in
Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable in
54
Foreach Loop Used to work on parts of a group, one at a time The loop control variable is created in the foreach statement The foreach automatically checks and changes the loop control variable foreach ( create loop control variable in group to work with )
55
Foreach Example
56
Foreach Example ArrayList myList = new ArrayList(); // create list
57
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list
58
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item
59
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item foreach ( ) { … }
60
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList { … }
61
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList { … } Create loop control variable
62
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in { … } Create loop control variable
63
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { … } Create loop control variable
64
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { … } Create loop control variable Automagically check & change loop control variable
65
Foreach Example ArrayList myList = new ArrayList(); // create list
myList.Add(4); // fill the list // Loop through the list item by item foreach ( int numFmList in myList) { if ( numFmList == matchNum ) … }
66
Task Add to your “loops” program: After your for loop…
Create an ArrayList and fill it with the first 5 integers Use a foreach loop to print the first 5 integers from the ArrayList Display each number on its own line with “Next #: Before the loop, display the message “Foreach loop”
67
Clear and Unclear Windows
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.