For each loops are great when we want to do something to each element of an array. But sometimes we only want to do something until a condition is met. For that, we can use a for
loop.
Let's refactor our GroceryList.cs
app to use a for
loop instead of for each
:
using System;
class GroceryList
{
static void Main()
{
string[] myGroceryList = { "eggs", "milk", "bread", "bananas", "cereal", "rice" };
Console.WriteLine("My grocery list:");
for (int index = 0; index < myGroceryList.Length; index++)
{
Console.WriteLine(myGroceryList[index]);
}
}
}
Here's how the for
loop works:
The for
statement here takes three parameters: initialization, condition, and final expression. Each are separated by semicolons ;
.
The initialization parameter (int index = 0
) creates an int
called index
that starts at zero. This states that the first time the loop runs is actually the 0th time. Initializing for
loops at 0 is a common practice, but they can theoretically be initialized at any number.
The condition parameter (index < myGroceryList.Length
) tells the loop when it should stop running. In this case, we've instructed our loop to halt when index
is no longer less than myGroceryList.length
. Keep in mind that myGroceryList.Length
evaluates to 6
, but the last index of myGroceryList
is 5
, because indexes begins at 0
.
The final expression parameter (index++
) manipulates the variable that keeps track of where we are in the loop. Here we use the increment operator ++
to add 1
to index
each time we go through the loop.
Within the loop, we include the line Console.WriteLine(myGroceryList[index]);
. On each loop through, we locate and print the item in myGroceryList
at the current index
.
Let's write an application that tells users the leap years that occurred during their lives. We'll begin with a LeapYear.cs
file containing the following:
using System;
class LeapYear
{
static void Main()
{
Console.WriteLine("What year were you born in?");
string stringBirthYear = Console.ReadLine();
int birthYear = int.Parse(stringBirthYear);
Console.WriteLine("You were alive during these leap years:");
for (int year = birthYear; year <= 2020; year ++)
{
if (year % 4 == 0)
{
Console.WriteLine(year);
}
}
}
}
Here we ask for the user's birth year and save the input into the variable birthYear
. In our for
loop, we:
year
variable, setting it to the user's birthYear
.year <= 2020
is no longer true
.1
to our year
for every iteration of the loop.For each iteration of the loop, we run this code:
if (year % 4 == 0)
{
Console.WriteLine(year);
}
For every year
divisible by 4
, the modulo comes out to 0
, which makes year % 4 == 0
evaluate to true
. Every year divisible by 4
is a leap year, so we print it for the user in Console.WriteLine(year);
. (There are some exceptions to this rule - we'll do an exercise later to deal with all the complexities of leap years!)
string[] myGroceryList = { "eggs", "milk", "bread", "bananas", "cereal", "rice" };
for (int index = 0; index < myGroceryList.Length; index++)
{
Console.WriteLine(myGroceryList[index]);
}
}
}
A for
statement here takes three parameters: initialization, condition, and final expression. Each are separated by semicolons ;
.
The initialization parameter (int index = 0
) creates an int
called index
that starts at zero. This states that the first time the loop runs is actually the 0th time. Initializing for
loops at 0 is a common practice, but they can theoretically be initialized at any number.
The condition parameter (index < myGroceryList.Length
) tells the loop when it should stop running.
The final expression parameter (index++
) manipulates the variable that keeps track of where we are in the loop.
Lesson 9 of 9
Last updated more than 3 months ago.