C# while Loops
int k = 1; stores 1 before any loop. Then while (k <= 3) keeps going while that test is true. Inside the braces, Console.WriteLine(k) writes the current number, and k = k + 1 stores one more under k. The screen shows three lines: 1, then 2, then 3. Keep the public class named Pine.
Start outside, test in while, step inside
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete any for loop if that line is still there, including for (int n = 1; n <= 3; n = n + 1) and every start-test-step packed into one header. Delete int choice if that line is still there. Delete any ReadLine line. The file you keep has no wait for typing, no for header with three jobs in parentheses, and no switch. The screen is written only by the writing line inside the while braces.
Inside Main, store the counter first: int k = 1;. That line is not inside the while. It only starts k at 1. Then the loop:
while (k <= 3) {
Console.WriteLine(k);
k = k + 1;
}
The word while is lowercase. Inside the parentheses sits only the test: k <= 3. There is no start and no step inside those parentheses — unlike the for line you saw earlier. The braces hold both the writing line and the step. Console.WriteLine(k); writes the current number with no quotes around k. Then k = k + 1; stores one more under k so the next test can eventually become false. Save the file.
The file you keep
Keep this teaching shape. One storing line above the loop, int k = 1;. One while with parentheses around the test only. One writing line inside the braces. One step inside the braces, k = k + 1;. There is no for header, no n counter from the previous page, and no quotes around k.
using System;
public class Pine
{
public static void Main()
{
int k = 1;
while (k <= 3)
{
Console.WriteLine(k);
k = k + 1;
}
}
}
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. int k = 1; stores 1. while (k <= 3) asks the test. Console.WriteLine(k); writes the current k. k = k + 1; moves the counter. Three lines on the screen: 1, then 2, then 3. There is no for 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 counter and the while were already in the file. If dotnet run waits, ReadLine is still in the file. Delete that line and keep int k = 1; and the while above. The screen shows 1, then 2, then 3. If the screen never stops and keeps writing 1, the step is missing from the braces. Put k = k + 1; back inside. If the screen shows a for-style run leftover, delete the for header. If the screen shows the letter k on every line, the writing line has quotes: Console.WriteLine("k");. You do not reinstall the .NET SDK for a screen that shows the wrong lines.
Walk one full run in order. k starts at 1 before the while. The test k <= 3 is true, so the body runs: the writing line writes 1, then the step stores 2. The test is still true: writes 2, stores 3. The test is still true: writes 3, stores 4. Now k <= 3 is false, so the loop ends. The start ran once, above the loop. The test ran before each pass. The step ran inside the braces after each write. Those three jobs are the same ideas as in for, but they are not packed into one header line. If you omit k = k + 1; and leave k at 1 forever, the test stays true and the same line can write without stopping — that endless case is why the step belongs in the body on this page.
Widen the test
Change only the test so the loop goes one step farther: while (k <= 4). Leave the start and the step as they are. Now the screen should show four lines: 1, 2, 3, and 4.
using System;
public class Pine
{
public static void Main()
{
int k = 1;
while (k <= 4)
{
Console.WriteLine(k);
k = k + 1;
}
}
}
This is the second file. It is not the file you keep. Run dotnet run. The screen shows four lines. You did not rewrite the writing line or the step — only the test inside while (…) changed. After you have seen four lines, put the kept file back: k <= 3 and the same body. Both versions are valid C#. They teach the same while shape with a different stopping point.
Read the four-line run once more. k starts at 1. Each true test writes the current k and then adds 1. When k becomes 5, the test fails and the loop stops. The digit 5 never appears on the screen in this demo, because the body only runs while the test is still true.
📊 k and the first demo screen
| k before WriteLine | Screen line |
|---|---|
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
Use the table as a check after the first demo, not instead of a run. Before each writing line in that demo, k holds the number that will appear on that line. After the third write, the step stores 4 and the test fails. If the screen never stops on a version without a step, put k = k + 1; back inside the braces — do not reinstall the .NET SDK.
Rules that keep while clear
Parentheses around the test are required. Writing while k <= 3 { … } without parentheses makes the build write an error. The body on this page always sits in braces, because two lines belong to each pass: the writing line and the step. If you leave the step after the closing brace, it runs once after the loop ends — and inside the loop k never changes, so the loop can run forever. Keep the step inside the braces in both demos.
while is not for and not switch. for packs start, test, and step into one header line. while keeps the test alone in its header and asks you to place the start and the step yourself. switch picks among case labels once. Do not leave a for loop or a switch in the same Main and wonder why the screen shows a for-style header leftover or the word mid. This session’s Main should hold the int and the while only.
A common mix-up is writing the syntax itself: Console.WriteLine("while");. That writes the characters while. It does not repeat. Another mix-up is putting quotes around k: Console.WriteLine("k"); writes the letter k on every pass. Keep Console.WriteLine(k) without quotes when you want the counter on the screen. If the test is already false when the while is first reached — for example k starts at 5 and the test says k <= 3 — the body never runs and the screen stays blank. That is still a valid while; this lesson’s demos keep a test that is true at least once so you can see the written lines.
You can now start a counter above while, put a test in while (…), and keep both the writing line and the step inside the braces. Changing the test changes how many lines you get. The next page is break and continue, where you leave a loop early or skip one pass without ending the whole loop.
The screen is the changing counter
The file you keep writes 1, then 2, then 3. The name k is in the writing line, and the letter k is not on the screen. The start sits above the while, and it is not on the screen. Quotes around k or around while write letters, not the counter path.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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