Lesson 7: Conditionals
Last updated
Last updated
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.
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.
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.
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!
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.
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.
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.
Question
What will be the output when this program is run?
Questions
True or False:
You can put arithmetic expressions (e.g. 10 + 5
) as the condition of an if statement.
An else clause can only exist as part of an if statement.
You can put an if statement inside another if statement.
You can't put an if statement inside an else clause.
Challenge
Let's make songs into code! Did you guess which popular song our sample code was about? If you guessed "If You're Happy And You Know It", you're correct!
For this challenge, find a popular song with a conditional and try to represent it as code! Turn the "if" part into an if statement, and the rest into a Console.WriteLine()
statement.
Example songs:
"If You Leave Me Now" by Chicago
If you leave me now, you'll take away the biggest part of me
"If I Ain't Got You" by Alicia Keys
But everything means nothing, if I ain't got you
"I Will Always Love You" by Whitney Houston
If I should stay, I would only be in your way
"Time After Time" by Cyndi Lauper
If you're lost, you can look and you will find me
If you fall, I will catch you, I will be waiting