What if-else does
An if statement runs a block only when its condition is true. When the condition is false, the program skips the block and carries on. That is often not enough: you want the program to do something in the other case too. An else branch gives you that second path.
Exactly one of the two blocks runs, never both and never neither. The condition decides which.
The syntax
bool amTired = false;
if (amTired) {
Console.WriteLine("I take a nap");
} else {
Console.WriteLine("I start coding");
}
Here amTired is false, so C# skips the first block and runs the one after else. The program prints:
I start coding
A few details about the shape:
- The condition goes in parentheses after
ifand must be abool. C# does not treat numbers or strings as true or false, soif (1)does not compile. elsehas no condition and no parentheses. It runs whenever theifcondition was false.elsealways belongs to anif. On its own it is a compile error.- Where you put
elseis a style choice. Both} else {on one line andelseon its own line are valid. This lesson uses the first form.
Going beyond two branches
When there are more than two outcomes, chain tests with else if. C# checks the conditions from top to bottom and runs the first block whose condition is true. A final plain else catches whatever is left:
int score = 7;
if (score >= 9) {
Console.WriteLine("A");
} else if (score >= 5) {
Console.WriteLine("B");
} else {
Console.WriteLine("C");
}
This prints B. The first condition, 7 >= 9, is false, so C# moves on; the second, 7 >= 5, is true, so it runs that block and skips the rest. Because the order matters, put the most specific test first.
Always use braces, even for a one-line body. C# allows an if without them, but then only the next single statement belongs to the branch, and adding a second line later is a classic way to introduce a bug.
The exercise, step by step
The lesson starts with a variable and an if that reports a pass:
bool passed = false;
if (passed) {
Console.WriteLine("Passed the test");
}
With passed set to false, this program prints nothing at all, which is a poor way to report a result. The fix has one step: attach an else branch that prints the failure message.
Put the else keyword right after the closing brace of the if block, open a new block, print with Console.WriteLine, and close the block with its own brace. The result:
bool passed = false;
if (passed) {
Console.WriteLine("Passed the test");
} else {
Console.WriteLine("Failed the test");
}
Now passed is false, so the program prints Failed the test. Change the first line to bool passed = true; and it prints Passed the test instead. Try both values in the editor above.
Common mistakes
- Putting a condition on
else.else (passed == false)is a syntax error. If you need a second test, writeelse if (...), which chains anotherifafter theelse. - Using
=instead of==in a condition.if (passed = true)assignstruetopassedand then tests it, so the first block always runs. The compiler warns about this. Compare values with==, or, for abool, just writeif (passed). - A stray semicolon. Writing
if (passed);ends the statement immediately, so the block that follows is no longer attached to theif, and a followingelsewill not compile. - Using two separate
ifstatements instead of oneif-else. Two independentifblocks each test their own condition, so both could run, or neither. Anelseguarantees that exactly one branch runs, because it is tied to the single condition above it. - Wrong capitalisation. C# is case-sensitive.
ElseandIfare not keywords, and the method isConsole.WriteLine, notconsole.writeline.
Key takeaways
ifruns a block when its condition is true;elseruns a block when it is false.- Exactly one of the two blocks runs each time.
- The condition in C# must be a
bool. elsetakes no condition and cannot appear without anif.