go · 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: Is it Saturday? An if asks a condition like that. If the answer is true, Go runs the lines inside. If it is false, Go walks straight past.
To ask whether two values match, use two equals signs, ==. One = puts a value in a box. Every condition gives a bool: true or false.
The lines that belong to the if sit inside curly brackets, called a . The { goes at the end of the if line, never on a line of its own.
package main
import "fmt"
func main() {
day := "Saturday"
fmt.Println(day == "Saturday")
if day == "Saturday" {
fmt.Println("Pancakes for breakfast!")
}
fmt.Println("Time to get dressed.")
} Lines after the closing } sit outside the if, so they run every time.
guess first
Pizza or soup? Guess what this shows.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
package main
import "fmt"
func main() {
lunch := "soup"
if lunch == "pizza" {
fmt.Println("Best day ever!")
}
fmt.Println("Lunch is served.")
}
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 has crashed. Can you fix it?
output
press run to see what happens
- Go stopped at the
ifline. Look at the sign between answer and "Paris". - One
=puts a value in a box. To ask whether two values match, you need two. - Change
=to==on line 5, 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. - Put the
{at the end of theifline.
new words in your collection
- condition
- block
This lesson isn’t saved. A free account remembers every lesson you finish.