Learn Go: Conditionals
Lesson 1 of 1
  1. 1
    We make decisions every day based on certain conditions. Is the alarm ringing? If so, turn it off. Is it raining? If so, bring an umbrella. Is the ice cream truck parked outside? If so, it’s time …
  2. 2
    What if…? What if we’re hungry? If it’s raining? If the alarm’s ringing? We would do something in response to these conditions. if statements work very similarly to our own decision-making proce…
  3. 3
    If we’re hungry we would go to eat something. But if we’re not hungry then we don’t. The idea is that we have a backup plan or something we can default to in case our condition isn’t met. We can p…
  4. 4
    So far we’ve been checking on boolean values (variable assigned a true or false value). But, we can check more than a single value using comparison operators. Here are two commonly used compariso…
  5. 5
    There are more comparison operators that we haven’t covered and they may seem familiar from math class: Operator | Meaning: — | — | Greater than = | Greater than or equal to Like the previ…
  6. 6
    In the previous exercises we checked one condition at a time. But what if we wanted to check multiple conditions at a time? To do so, we can use logical operators. There are three logical operato…
  7. 7
    Our last logical operator is the not (!) operator. It negates (reverses) the value of a boolean. For example: bored := true fmt.Println(!bored) // Prints false tired := false; fmt.Println(!tir…
  8. 8
    We can add different conditions to our if…else statements using an else if statement. Adding an else if statement allows us to check another condition after our if statement checks its condition….
  9. 9
    else if statements are great for checking multiple conditions. However, we can find ourselves needing to check so many conditions that writing all the necessary else if statements can feel tedious….
  10. 10
    We can also include a short variable declaration before we provide a condition in either if or switch statments. Here’s the syntax: x := 8 y := 9 if product := x * y; product > 60 { fmt.Println(…
  11. 11
    Previously, we used hard coded values (values that don’t changed) and then created conditionals that checked on these values. For example: alarmRinging := true if alarmRinging { fmt.Println(…
  12. 12
    Previously, we saw how our random numbers weren’t entire random. The reason for this behavior is due to how Go seeds or chooses a number as a starting point for generating random numbers. By defa…
  13. 13
    If you’ve made it to this exercise, then you’ve finished Go’s conditionals lesson, great job! Here are the topics covered In this lesson: * How to create an if statement that checks a condition an…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo