C# if and else
bool ready = true; stores the word true. Then if (ready) looks at that word and runs one group of lines. When ready is true, Console.WriteLine("pass") writes pass. When ready is false, the else group runs and writes wait. The screen is one line. Keep the public class named Pine.
A bool decides which block runs
Open the Pine file. The class name stays Pine. This page does not rename the type. Delete bool both if that line is still there, including true && true, true || false, !true, and every combining mark between two true or false words. Delete bool match if that line is still there. Delete int total if that line is still there. Delete any ReadLine line. The file you keep has no wait for typing, no both, no match, and no bare writing of a combined word alone. The screen is written only by a writing line inside the chosen braces.
Inside Main, store a bool before you choose a path: bool ready = true;. bool is the kind word. ready is the name, and it starts with a small r. There are no quotes around true. The semicolon ends the storing line. That line does not write the screen. After that comes the choice:
if (ready) {
Console.WriteLine("pass");
} else {
Console.WriteLine("wait");
}
The word if is lowercase: i, then f. It is not a name you invent. A space after if is fine; if(ready) without a space also builds. Inside the parentheses sits the name ready — no quotes, no ==, no &&. When ready holds true, C# enters the first pair of braces and runs Console.WriteLine("pass");. The quotes around pass mark letters for the screen. Those letters — p, a, s, s — are what you should see, one line. The else block is skipped entirely on this run. Save the file.
The file you keep
Keep this teaching shape. One kind word, bool. One name, ready, stored as true. One if with parentheses around ready. One writing line that writes pass inside the first braces. One else with a writing line that writes wait. There is no both line, no match line, and no writing of the bare word true outside the braces.
using System;
public class Pine
{
public static void Main()
{
bool ready = true;
if (ready)
{
Console.WriteLine("pass");
}
else
{
Console.WriteLine("wait");
}
}
}
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. bool ready = true; stores true. It writes nothing. if (ready) asks whether that stored word is true. It is, so Console.WriteLine("pass"); runs. The else braces are skipped. One line on the screen: pass. No quotes on the screen. No second line. There is no bool both line in the file you keep.
Run it with dotnet run. The program writes pass. It does not wait for you to type. The stored word and the choice were already in the file. If dotnet run waits, ReadLine is still in the file. Delete that line and keep bool ready = true; and the if / else above. The program finishes after the chosen writing line. The screen shows pass. One line. If the screen shows true, you are still writing the bool itself, or a logical-page combine is still in Main. Replace that with Console.WriteLine("pass") inside the if braces when ready is true. If the screen shows wait, ready is false, which is the second file on this page, not the file you keep. If the screen shows if (ready) as letters, a writing line has quotes around those characters. You do not reinstall the .NET SDK for a screen that shows the wrong line.
Why pass and not wait? Because the test in parentheses was true. C# does not write the word true on this run. It writes whatever writing line runs inside the chosen braces. Think of three jobs: parentheses hold the test, braces group the lines that belong together, and Console.WriteLine is the only part that writes to the screen. A screen that shows pass does not, by itself, prove the if. Look for if (ready) and the braces in the file. Do not judge the file from the screen alone.
When ready is false
Change only the stored value: bool ready = false;. Leave the if / else structure as it is. Now the test fails, so C# skips the first braces and runs the else braces instead. The screen should show wait, one line.
using System;
public class Pine
{
public static void Main()
{
bool ready = false;
if (ready)
{
Console.WriteLine("pass");
}
else
{
Console.WriteLine("wait");
}
}
}
This is the second file. It is not the file you keep. Run dotnet run. The screen shows wait. One line. It does not show pass. It does not show false as the screen word unless you write that word on purpose. If the screen shows pass, you are still on the file you keep, where ready is true. After you have seen wait, put the kept file back: bool ready = true; and the same if / else that writes pass on the true path. Both versions are valid C#. They teach the same if / else shape with opposite tests.
Read the false run once more in order. ready holds false. if (ready) is asked. The answer is false, so every line between the first { and its matching } is skipped — including Console.WriteLine("pass");. Control continues at else. The lines between the else braces run, so Console.WriteLine("wait"); executes. One line on the screen. No second line. The word false itself never appears unless you write it on purpose.
Rules that keep the choice clear
Parentheses around the test are required. Writing if ready { … } without parentheses makes the build write an error. The test on this page is a bool: a name like ready, or the bare words true or false with no quotes. if ("true") puts letters where a bool test belongs; the build writes an error. Remove the quotes, or keep using a bool name. A whole number alone such as if (1) is not how this lesson stores readiness; stay with true, false, or a bool name.
Braces matter for reading the lesson. This page always wraps each path in { }. C# allows a one-line if without braces, but the moment you add a second line under that if, only the first line stays tied to the test unless braces are there. Beginners lose that detail easily. Keep braces in both demos so each path is obvious: one opening brace after the test, one closing brace before else, then another pair for else.
else belongs to an if. A lone else makes the build write an error. On this page one if has one else. You may write if without else when you only care about the true path; the file you keep still shows both sides so you can see the fork. Choosing among many fixed values is switch on a later page — do not invent a third branch with another else here.
You can also write the test without a separate name: if (true) { … } else { … }. That builds and runs. The demos use ready so you see a stored bool driving the choice, which is how programs look after you already know how to store true and false. Comparisons such as if (score > 10) belong after you are comfortable with this true / false fork; this page keeps the test as a bool value you already understand.
📊 ready and the screen
| ready | Screen |
|---|---|
| true | pass |
| false | wait |
Use the table as a check after a run, not instead of one. true with the file you keep writes pass. false with the second file writes wait. If both writing lines somehow run, the braces or the if / else pairing is wrong — fix the structure rather than reinstalling the .NET SDK.
A common mix-up is writing the syntax itself: Console.WriteLine("if (ready)");. That writes the characters if (ready). It does not choose a path. Another mix-up is leaving && or == demos in the same Main and wondering why the screen shows true instead of pass. This session’s Main should hold the bool and the if / else only.
You can now store a bool, put it in if (…), and give each outcome its own braced block. When the test is true, one writing line runs; when it is false, the else writing line runs. The next page is switch, where several fixed values each get their own case instead of a single true / false fork.
The screen is the chosen path
The file you keep writes pass. The name ready is in the parentheses, and ready is not on the screen. The word true is in the storing line, and true is not on the screen. The else path is in the file, and it does not run on the kept run. Quotes around if (ready) write letters, not a chosen 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