c# · level 1 · unit 4 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An if runs code only when its is true.
Picture a fork in the path with a sign asking a question. An if asks a condition like that, inside round brackets. If the answer is yes, C# runs the lines inside. If not, it walks straight past.
Every condition is true or false. A value that can only be one of these is a , and it can live in a box too. To ask whether two values match, use two equals signs, ==.
The lines that belong to the if sit in a , between curly brackets that each get a line of their own. Indent the lines inside by four spaces, so your eyes can see the block too.
using System;
int goals = 3;
Console.WriteLine(goals == 3);
if (goals == 3)
{
Console.WriteLine("Hat-trick! Take a bow.");
}
Console.WriteLine("Full time."); On the screen, C# shows a bool with a capital letter, as True or False.
guess first
Rain or shine? Guess what this shows.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
bool raining = false;
if (raining)
{
Console.WriteLine("Take a coat.");
}
Console.WriteLine("Off you go!");
your turn
Your turn. Get past the robot door.
The robot only opens for its owner. Type your name in both boxes, spelled exactly the same, then press run.
output
press run to see what happens
fix it
The quiz will not run. Can you fix it?
output
press run to see what happens
- C# stopped at the
ifline. Look at the sign betweenanswerand12. - One
=puts a value in a box. To ask whether two values match, you need two. - Change
=to==on line 4, then press run.
nailed it
your code can make choices now. :D
ifruns its block only when the condition istrue.- Use
==to ask whether two values match. - The block sits between curly brackets, indented.
new words in your collection
- condition
- bool
- block
This lesson isn’t saved. A free account remembers every lesson you finish.