Missing Document Title
theme: Next,1
[fit] Looping
Perform a task repeatedly or a certain number of times
Perform a task repeatedly until a condition is met
Process all of the items in a collection
We've seen this with the while
control flow:
Do something 10 times.
var counter = 0;while (counter < 10) {Console.WriteLine("Doing something");counter++;}
^ counter++
is a shortcut for counter = counter + 1
Introducing for
loop
// Do this at the start// |// | Keep going as long as this is true// | |// | | Do this after each loop is done// | | |// | | |// V V Vfor(INITIALIZATION; CONDITION; AFTERTHOUGHT) {// Loop statements}
[fit] Revisiting printing something 10 times
// Start the counter at 0// |// | Keep going as long as counter is less than 10// | |// | | Increment counter after each loop is done// | | |// | | |// V V Vfor (var counter = 0; counter < 10; counter++) {Console.WriteLine("Doing something");}
^ Another way to read the for loop is Start the counter at 0 and as long as the value of counter is less than 10, do the contents of the loop and then increment counter.
Looping through a List
var names = new List<string>() { "Mark", "Paula", "Sandy" };
Let's loop through this list and print out each name.
// Start the index at 0// |// | Keep going as long as index is less than the length of the list// | |// | | Increment index after each loop is done// | | |// | | |// v v vfor (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
Pretending we are .NET
It is often helpful to imagine we are .NET and walk through the code and see it how the computer does
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
[.code-highlight:1]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Make a List of three strings.
names => "Mark", "Paula", "Sandy"
[.column]
[.code-highlight:3]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
First time at this loop. Do the initialization. Make index
equal to 0
[.code-highlight:2]
names => "Mark", "Paula", "Sandy"index => 0
[.column]
[.code-highlight:4]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Make currentName
equal to whatever is at the index given by index
. Well index
is 0
so that makes currentName
equal to "Mark"
[.code-highlight:3]
names => "Mark", "Paula", "Sandy"index => 0currentName => "Mark"
[.column]
[.code-highlight:6]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Call the method Console.WriteLine
and provide it the value in currentName
which is currently "Mark"
[.code-highlight:3]
names => "Mark", "Paula", "Sandy"index => 0currentName => "Mark"
[.column]
[.code-highlight:7]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
End of the loop, so do the afterthought step of index++
, turning the value of 0
to 1
[.code-highlight:2]
names => "Mark", "Paula", "Sandy"index => 1currentName => "Mark"
[.column]
[.code-highlight:3]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Back to the loop. Since currentName
is defined inside the loop we forget that variable. Time to do the comparison. Is index
(1
) less than names.Count
(3
). Yes, so do the loop again.
[.code-highlight:2]
names => "Mark", "Paula", "Sandy"index => 1
[.column]
[.code-highlight:4]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Make currentName
equal to whatever is at the index given by index
. Well index
is 1
so that makes currentName
equal to "Paula"
[.code-highlight:3]
names => "Mark", "Paula", "Sandy"index => 1currentName => "Paula"
[.column]
[.code-highlight:6]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Call the method Console.WriteLine
and provide it the value in currentName
which is currently "Paula"
[.code-highlight:3]
names => "Mark", "Paula", "Sandy"index => 1currentName => "Paula"
[.column]
[.code-highlight:7]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
End of the loop, so do the afterthought step of index++
, turning the value of 1
to 2
[.code-highlight:2]
names => "Mark", "Paula", "Sandy"index => 2currentName => "Paula"
[.column]
[.code-highlight:3]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Back to the loop. Since currentName
is defined inside the loop we forget that variable. Time to do the comparison. Is index
(2
) less than names.Count
(3
). Yes, so do the loop again.
[.code-highlight:2]
names => "Mark", "Paula", "Sandy"index => 2
[.column]
[.code-highlight:4]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Make currentName
equal to whatever is at the index given by index
. Well index
is 2
so that makes currentName
equal to "Sandy"
[.code-highlight:3]
names => "Mark", "Paula", "Sandy"index => 2currentName => "Sandy"
[.column]
[.code-highlight:6]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Call the method Console.WriteLine
and provide it the value in currentName
which is currently "Sandy"
[.code-highlight:3]
names => "Mark", "Paula", "Sandy"index => 2currentName => "Sandy"
[.column]
[.code-highlight:7]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
End of the loop, so do the afterthought step of index++
, turning the value of 2
to 3
[.code-highlight:2]
names => "Mark", "Paula", "Sandy"index => 3currentName => "Paula"
[.column]
[.code-highlight:3]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
Back to the loop. Since currentName
is defined inside the loop we forget that variable. Time to do the comparison. Is index
(3
) less than names.Count
(3
). No! So we leave the loop, moving to the code AFTER the loop
[.code-highlight:2]
names => "Mark", "Paula", "Sandy"index => 3
[.column]
[.code-highlight:9]
var names = new List<string>() { "Mark", "Paula", "Sandy" };for (var index = 0; index < names.Count; index++) {var currentName = names[index];Console.WriteLine(currentName);}Console.WriteLine("Hi, this is code after the loop");
[.column]
We are now out of the loop, and since index
was defined inside the loop we forget about that variable as well.
names => "Mark", "Paula", "Sandy"
[fit] Whew!
shorter syntax
foreach
foreach (var name in names) {Console.WriteLine(name);}
^ Assumes you are going to loop through the entire list.