if and else
What you'll learn:
- choose which code runs with
if,else if,elseand the conditional operator?:; - make a choice by value with the
switchstatement and theswitchexpression; - repeat code with
while,doandforloops and control them withbreakandcontinue; - read nested loops and tell which loop
breakleaves.

Picture a fork in the road with a signpost: "If it's hot, go left, to the river. Otherwise go right, to the park." You read the condition and pick one road, and this time you don't take the other one.
So far, the programs in this course have run all their statements in a row, from top to bottom. But a program needs forks too: one answer for hot weather, another for cold.
Such a choice is written with the if statement. After it, the parentheses hold a condition, and the braces hold code that runs only when the condition is true:
int t = 20;
if (t > 30)
{
Console.WriteLine("hot");
}
else if (t > 15)
{
Console.WriteLine("warm"); // warm
}
else
{
Console.WriteLine("cold");
}Let's walk through the program step by step:
int t = 20;puts the temperature outside into the variablet.- The condition
t > 30is false:20is not greater than30. The code in the braces under it is skipped. - The condition
t > 15is true, so the code underelse ifruns, and the program printswarm. - The program never gets to
else: one branch has already run.
With t = 35, the program prints hot. With t = -5, both conditions are false, and the last part runs: the program prints cold.
A condition is a bool
A condition is an expression of type bool: its value is true or false. Usually it is a comparison like t > 30, or several comparisons joined with && and ||:
int age = 20;
bool hasTicket = true;
if (age >= 18 && hasTicket)
{
Console.WriteLine("Welcome"); // Welcome
}Here age >= 18 is true and hasTicket is true, so the whole condition is true, and the program prints Welcome. Without a ticket, the program would print nothing: this if has no else, and when the condition is false the block is skipped entirely.
A number or a string cannot be a condition. if (t) does not compile: C# does not turn an int into a bool on its own. You need a comparison, such as if (t != 0).
For the same reason, the typo if (t = 30) does not compile either. As you remember from the lesson on comparisons, a single = is assignment, and the value of such code is the number 30, not a bool.
Three kinds of branches
The code in braces after if, else if or else is called a branch. The first example has three of them, and each has its own role.
The if branch
The if branch opens the fork. An if is written in three parts:
- the keyword
if; - a condition in parentheses, for example
(t > 30); - a block in braces that runs when the condition is true.
If the condition is false, the program skips the block and moves on. An if can also stand without else, as in the ticket example.
The else if branch
An else if branch adds one more condition to the fork. There can be any number of them between if and else.
The conditions are checked from top to bottom, and only the first branch with a true condition runs. With t = 35, both t > 30 and t > 15 are true, but the program prints only hot.
The order of branches matters. If you swap the branches with t > 30 and t > 15, the program prints warm for t = 35: the first true condition is now t > 15. The stricter condition goes higher.
This is where the road analogy breaks down. A traveler can read every signpost and pick the best one. The program follows the first signpost with a true condition and does not read the rest.
The else branch
The else branch has no condition. It runs when every condition above it is false, and it always comes last.
If a branch holds one statement, you may leave out the braces. In this course's code they are always there, each on its own line: that way the branch's borders are visible at once.
The conditional operator ?:
Sometimes you need to choose a value rather than code: one of two strings or one of two numbers. That is what the conditional operator ?: is for. It is also called the ternary operator, because it has three parts: condition ? a : b. If the condition is true, the whole expression equals a; otherwise it equals b:
int age = 16;
string status = age >= 18 ? "adult" : "minor";
Console.WriteLine(status); // minorLet's go step by step:
- The condition
age >= 18is false:16is less than18. - So the whole expression equals the second value,
"minor", and that goes intostatus. Console.WriteLine(status)printsminor.
Both values must fit the variable on the left. In status, of type string, you can choose one of two strings, but the compiler rejects a number in place of "minor".
if is a statement: it decides which code runs. ?: is an expression: it has a value, which you can store in a variable or pass straight to Console.WriteLine. For three or more options, an if and else if chain is easier to read.
Common mistake
A semicolon follows the condition:
int t = 10;
if (t > 30); // ✗
{
Console.WriteLine("hot");
}warning CS0642: Possible mistaken empty statementThe program compiles with this warning and prints hot, even though 10 is not greater than 30. The message means that an empty statement may have been put there by mistake.
The semicolon ended the if with an empty statement: with a true condition, the if runs it and does nothing. The block in braces no longer belongs to the if and always runs. Remove the ; after (t > 30), and the block becomes a branch again.
Key points
ifruns its branch when the condition is true, andelseruns when every condition above it is false.- An
else ifchain checks the conditions from top to bottom and runs only the first matching branch. - A condition is a
boolvalue: C# does not accept a number or a string as a condition. - The conditional operator
condition ? a : bpicks one of two values.