Tuning the header
A for loop is controlled by three parts: initialization, condition and iteration. Changing one of them changes which numbers the loop visits. This lesson changes the first two, to print a range that does not start at 0.
A range that starts at 2
for (int i = 2; i < 7; i++) {
Console.WriteLine(i);
}
The output is:
2
3
4
5
6
int i = 2makes the first value printed 2.i < 7keeps the loop going whileiis 6 or less, so 6 is the last value printed. 7 fails the test and is never printed.i++adds 1 after each pass.
Two ways to write the same upper bound
For whole numbers, i < 7 and i <= 6 are equivalent: both stop after 6. Pick the one that matches how you think about the task:
- Use
<=when the last value is named in the task ("through 10"), so the number in the code is the number in the task. - Use
<when you count items from 0, such asi < nfor the firstnpositions.
A quick way to know how many passes a loop makes: for a step of 1, it is the last value minus the first value plus 1. From 2 through 6, that is 6 - 2 + 1 = 5 passes.
The exercise: 3 through 10
The task is to print every number from 3 to 10, including both ends. It takes two steps.
- Write the header. Declare
int i = 3so that the first value is 3. Usei <= 10so that 10 is included: withi < 10the loop would stop at 9. Finish withi++. - Print the counter. Call
Console.WriteLine(i)in the body.WriteLineadds a line break after the value, so each number gets its own line.
for (int i = 3; i <= 10; i++) {
Console.WriteLine(i);
}
This prints 3, 4, 5, 6, 7, 8, 9 and 10, eight lines in total, which matches 10 - 3 + 1.
Common mistakes
- Dropping the endpoint. Writing
i < 10when the task says "through 10" misses the last number. - Printing a string instead of the variable.
Console.WriteLine("i")prints the letter i every time. Without quotes, it prints the value. - Using
Console.Writeby accident. It prints without a line break, so the numbers run together on one line as345678910. - Starting from the wrong value. Initialization sets the first number printed. Check it against the task before anything else.
Other starting points
The initialization part can hold any int, including negative numbers and values from variables:
for (int i = -2; i <= 2; i++) {
Console.Write(i + " ");
}
// -2 -1 0 1 2
int start = 3, end = 10;
for (int i = start; i <= end; i++) {
Console.Write(i + " ");
}
// 3 4 5 6 7 8 9 10
Console.Write keeps the output on one line, which is handy for checking a range quickly. If start were larger than end, the condition would be false on the first check and the body would never run.
Checking a loop before you run it
Three quick questions catch most range mistakes:
- What is the first value? It is whatever the initialization sets.
- What is the last value? It is the largest number that still passes the condition.
- How many lines should appear? For a step of 1, it is last minus first plus 1.
For the exercise, the answers are 3, 10 and 8. If your output has seven lines or ends at 9, the condition is off by one.
When the loop never runs
If the condition is false on the very first check, the body is skipped completely and the program moves on without an error. For example, for (int i = 10; i <= 3; i++) prints nothing. When a loop produces no output at all, compare the starting value with the limit first.
Key takeaways
- Initialization sets the first value; the condition decides where the loop stops.
- For integers,
i < 7andi <= 6stop at the same place. - Choose
<=when a task says "through" a number.