Skip to Content
Learn
Learn Go: Conditionals
Review

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 and executes code if the condition is true
  • How to add an else if statement to check for additional conditions.
  • The use of an else statement that runs by default if none of the previous conditional statements evaluated to true.
  • Comparison operators check between two operands and evaluate to a boolean.
  • Logical operators check between two boolean values and return a single boolean.
  • switch statements can be used to check between multiple conditions much like an if… else if… else statement.
  • Short variable declarations can be used prior to providing a condition for either if or switch statements. Declared variables are scoped to the statement blocks.
  • The math/rand library’s .Intn() method is used to generate random numbers.
  • Go uses a seed value to as a starting point for generating random numbers.
  • Unique seed values can be created using time, namely rand.seed(time.Now().UnixNano())

Including conditionals in allows you to add a layer of logic to your code to address many different scenarios. It’s an important foundational skill to have under your belt as a developer.

Instructions

If you want to challenge yourself, try writing an if… else if… else and/or switch statement from scratch. Here are a few ideas to write the statements for:

  • Use comparison operators in a switch statement.
  • Create a numeric grade to letter grade conditional (or vice versa).
  • Create conditionals for a flow chart, e.g. what to do/check if your computer won’t power on.
  • Create a conditional that prints the price of an item when a condition is matched.
Folder Icon

Take this course for free

Already have an account?