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
ifstatement that checks a condition and executes code if the condition is
- How to add an
else ifstatement to check for additional conditions.
- The use of an
elsestatement that runs by default if none of the previous conditional statements evaluated to
- Comparison operators check between two operands and evaluate to a boolean.
- Logical operators check between two boolean values and return a single boolean.
switchstatements can be used to check between multiple conditions much like an
if… else if… elsestatement.
- Short variable declarations can be used prior to providing a condition for either
switchstatements. Declared variables are scoped to the statement blocks.
.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
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.
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
- 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.
Take this course for free