Lesson 7: Conditionals
At the end of this lesson, you should be able to answer the following:
How do I write a conditional statement in C#?
What is an else clause?
In life, we make decisions every moment. We can show this decision making process through a programming construct called a conditional.
If statement
var happy = true;
if (happy)
{
Console.WriteLine("Clap your hands!");
}
Console.WriteLine("END");The code above contains an if statement block. If the condition in the round brackets is true, the program will execute the statements in the curly brackets. Otherwise, it will just skip the block and go to the next statement after it.
Type the code above in the code box and run it. We should see two lines in the output, Clap your hands! followed by END.

Change the value of the variable happy to false, then run the program again.

Now we don't see the Clap your hands! line printed anymore. This is because our condition is the value of happy. Since it is now false, our program does not run the lines inside the curly braces. Instead, it goes to the next statement, Console.WriteLine("END");.
We can also use expressions as the condition in the round brackets, as long as the expression's result is a bool value. Recall from Lesson 3 that bool or Boolean values have only two possible values, true or false. Lesson 4 has a list of comparison and logical operators that we can use to make expressions.
var password = "abc123";
if (password.Length < 8)
{
Console.WriteLine("Your password is too short!");
} The example above checks the length of a password. In Lesson 6 we learned how to get the length of a string, by calling .Length. The result is an int value which we can now compare with another integer, 8.
We can even nest if statements, by putting another if statement inside the statement block.
Type the code below into a code box and run the program. Try changing the values of passedFirstTest, passedSecondTest, and passedThirdTest to see the different outputs you can make.
var passedFirstTest = true;
if (passedFirstTest)
{
Console.WriteLine("You've passed the first test!");
var passedSecondTest = true;
if (passedSecondTest)
{
Console.WriteLine("You've passed the second test!");
var passedThirdTest = true;
if (passedThirdTest)
{
Console.WriteLine("Congratulations! You passed all the tests!");
}
}
}
As you may have noticed, we indent the statements inside the curly brackets. It's not required by C#, but it makes it easier to see which statements are part of which if block. Otherwise, the code can become unreadable, causing all sorts of program errors!
Else clause
When we make a choice, often there is an alternate path that happens if the condition isn't true. We can show this in our code through the else clause. The else clause is an optional addition to an if statement.
An if statement doesn't always have to have an else clause, but an else clause can't exist without an if statement.
var age = 20;
if (age < 16)
{
Console.WriteLine("You're not allowed to drive yet.");
}
else
{
Console.WriteLine("You're allowed to drive.");
}The code above is checking the value of age. If age is less than 16, the text You're not allowed to drive yet is displayed. Otherwise, You're allowed to drive is shown.

We can also nest another if statement inside the else clause, if we wanted to check for more conditions.
var age = 20;
var hasLicence = false;
if (age < 16)
{
Console.WriteLine("You're not allowed to drive yet.");
}
else
{
if (hasLicence)
{
Console.WriteLine("You're allowed to drive.");
}
else
{
Console.WriteLine("You need to have a licence to drive.");
}
}
C# has special syntax to make this code more concise and readable. We can put the nested if statement on the same level as the else, creating an else-if clause.
var age = 20;
var hasLicence = false;
if (age < 16)
{
Console.WriteLine("You're not allowed to drive yet.");
}
else if (hasLicence)
{
Console.WriteLine("You're allowed to drive.");
}
else
{
Console.WriteLine("You need to have a licence to drive.");
}
Last updated
Was this helpful?