Loading...

C# for Loops

for (int n = 1; n <= 3; n = n + 1) starts n at 1, keeps going while n is less than or equal to 3, and adds 1 to n after each pass. Inside the braces, Console.WriteLine(n) writes the current number. The screen shows three lines: 1, then 2, then 3. Keep the public class named Pine.

Three parts sit in the for line

Open the Pine file. The class name stays Pine. This page does not rename the type. Delete int choice if that line is still there, including every switch, case, break, and default from the previous page. Delete bool ready if that line is still there. Delete any ReadLine line. The file you keep has no wait for typing, no choice, and no switch. The screen is written only by the writing line inside the for braces.

Inside Main, write the loop:
for (int n = 1; n <= 3; n = n + 1) {
Console.WriteLine(n);
}
The word for is lowercase. Inside the parentheses sit three jobs, separated by semicolons. First: int n = 1; — make a whole-number name n and start it at 1. Second: n <= 3 — keep going while that test is true. Third: n = n + 1 — after each pass through the braces, store one more under n. The braces hold the body. Console.WriteLine(n); writes the current value of n and ends the line. There are no quotes around n on that writing line, so the number appears, not the letter n. Save the file.

The file you keep

Keep this teaching shape. One for with parentheses around start, test, and step. One name, n, starting at 1. One test, n <= 3. One step, n = n + 1. One writing line inside the braces that writes the bare name. There is no choice line, no switch, and no quotes around n.

n from 1 to 3

csharp
using System;

public class Pine
{
    public static void Main()
    {
        for (int n = 1; n <= 3; n = n + 1)
        {
            Console.WriteLine(n);
        }
    }
}

Read it from the top. using System; prepares short names. public class Pine names the type. public static void Main() is where the runtime starts. for (int n = 1; n <= 3; n = n + 1) sets up the three jobs. Console.WriteLine(n); writes the current n each time the body runs. Three lines on the screen: 1, then 2, then 3. No quotes. There is no int choice line in the file you keep.

Run it with dotnet run. The program writes three lines and stops. It does not wait for you to type. The loop was already in the file. If dotnet run waits, ReadLine is still in the file. Delete that line and keep the for above. The screen shows 1 on the first line, 2 on the second, 3 on the third. If the screen shows mid or none, the switch file is still in place. Delete the choice switch. If the screen shows the letter n on every line, the writing line has quotes around the name: Console.WriteLine("n");. Those quotes mark letters, so the counter is not written. If the screen shows for as letters, a writing line has quotes around that word. You do not reinstall the .NET SDK for a screen that shows the wrong lines.

Walk one full run in order. n starts at 1. The test n <= 3 is true, so the body runs and Console.WriteLine(n) writes 1. Then the step n = n + 1 stores 2. The test is still true, so the body writes 2. The step stores 3. The test is still true, so the body writes 3. The step stores 4. Now n <= 3 is false, so the loop ends. Nothing else writes. You do not write three separate writing lines by hand; the same body runs three times with a changing n. The start runs once at the beginning. The test runs before each pass. The step runs after each pass. Those three jobs stay in the parentheses; the writing job stays in the braces.

Raise the upper bound

Change only the test so the loop goes farther: for (int n = 1; n <= 5; n = n + 1). Leave the start and the step as they are. Now the screen should show five lines: 1, 2, 3, 4, and 5.

n from 1 to 5

csharp
using System;

public class Pine
{
    public static void Main()
    {
        for (int n = 1; n <= 5; n = n + 1)
        {
            Console.WriteLine(n);
        }
    }
}

This is the second file. It is not the file you keep. Run dotnet run. The screen shows five lines. You did not rewrite the writing line — only the test inside the for parentheses changed. After you have seen five lines, put the kept file back: n <= 3 and the same body. Both versions are valid C#. They teach the same for shape with a different stopping point.

Read the five-line run once more. n starts at 1. Each true test writes the current n and then adds 1. When n becomes 6, the test fails and the loop stops. The digit 6 never appears on the screen in this demo, because the body only runs while the test is still true.

Rules that keep the loop clear

Parentheses around the three parts are required. Writing for int n = 1; n <= 3; n = n + 1 { … } without the outer parentheses makes the build write an error. Semicolons separate the three parts inside those parentheses. The body on this page always sits in braces. C# allows a one-line body without braces, but a second line under the for stays outside the loop unless braces wrap both lines. Keep braces in both demos so the body is obvious.

for is not switch and not if. switch (choice) picks among fixed case labels once. if (ready) chooses one of two paths from true or false. for repeats the same braced body while a counter moves. Do not leave a switch or an if in the same Main and wonder why the screen shows mid or pass instead of 1, 2, 3. This session’s Main should hold the for loop only.

A common mix-up is writing the syntax itself: Console.WriteLine("for");. That writes the characters for. It does not repeat. Another mix-up is putting quotes around n in the body: Console.WriteLine("n"); writes the letter n on every pass, not the changing number. Keep Console.WriteLine(n) without quotes when you want the counter on the screen.

The step n = n + 1 can also be written n++ in later code you will see elsewhere. This page keeps n = n + 1 so every mark on the line is a word or number you already know how to read. The start can begin at 0 instead of 1 when a lesson needs that; here both demos start at 1 so the first written line matches the first count people say out loud. If the test is already false at the start — for example n starts at 1 and the test says n <= 0 — the body never runs and the screen stays blank. That is still a valid for; this lesson’s demos keep a test that is true at least once so you can see the written lines.

You can now write a for with a start, a test, and a step, put a writing line in the braces, and watch the counter appear on successive lines. Changing the test changes how many lines you get. The next page is while, where the test sits in its own place and the step is written inside the body instead of inside the for parentheses.

The screen is the changing counter

The file you keep writes 1, then 2, then 3. The name n is in the writing line, and the letter n is not on the screen. The three parts are in the parentheses, and they are not on the screen as a header line. Quotes around n or around for write letters, not the counter path.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top