Lesson 4

Understanding Go Comparison Operators: A Dive into Conditional Logic

Lesson Introduction and Plan

Greetings! Today, we're venturing into the rewarding realm of Go comparison operators. These operators are of paramount importance as they control the flow of code by enabling value comparisons.

Our goal for this session is to deepen our understanding of the usage of comparison operators in Go programs. We will investigate various Go comparison operators and reinforce your learning through real-world examples.

Exploring Go Comparison Operators

Imagine operating a submarine in the deep sea. Here, you determine your routes by evaluating conditions such as the distances to underwater landmarks. These decisions boil down to comparisons, similar to situations encountered in programming. In Go, we use comparison operators to facilitate such rational decision-making.

The Go programming language includes six comparison operators: equal to (==), not equal to (!=), greater than (>), less than(<), greater than or equal to (>=), and less than or equal to (<=). These operators return either true or false, also known as Boolean values.

Consider this comparison of a submarine's speed in relation to an ocean current as an example:

Go
1var submarineSpeed = 30 // speed in knots 2var currentSpeed = 20 // speed in knots 3fmt.Println("Is the submarine faster than the ocean current? ", submarineSpeed > currentSpeed) 4// Prints: Is the submarine faster than the ocean current? true

In the above code, we used the > operator to compare the submarineSpeed and the currentSpeed. The result is true because the submarineSpeed is greater than the currentSpeed.

Exploring == and != Operators

Now, let's delve into the equal to (==) and not equal to (!=) operators. These become crucial when you need to compare values, such as when comparing the current oxygen level to the desired one:

Go
1var currentOxygenLevel = 70 // current oxygen level in % 2var requiredOxygenLevel = 100 // required oxygen level in % 3 4var isOxygenEnough = currentOxygenLevel == requiredOxygenLevel // this results in 'false' 5var isOxygenLow = currentOxygenLevel != requiredOxygenLevel // this results in 'true'

The == operator checks whether the currentOxygenLevel equals the requiredOxygenLevel, yielding a false result. Conversely, the != operator verifies their inequality, returning true.

Exploring <, >, <=, and >= Operators

Next, let's examine the less than (<), greater than (>), less than or equal to (<=), and greater than or equal to (>=) operators. These operators are primarily used for numeric data comparisons. Suppose you're surveying two underwater caves and want to determine which one is closer. You can utilize these operators to make an informed decision:

Go
1var distanceToCaveA = 2000 // distance in meters 2var distanceToCaveB = 1000 // distance in meters 3 4var isACloser = distanceToCaveA < distanceToCaveB // this results in 'false' 5var isBCloserOrSame = distanceToCaveA >= distanceToCaveB // this results in 'true'

Here, we compare the distances to two underwater caves. The submarine is not closer to cave A, so isACloser is false. However, the submarine is closer to, or at the same distance from, cave BisBCloserOrSame is true.

Lesson Summary

Excellent work! We've embarked on an exploration of the Go comparison operators and their practical applications. Armed with this knowledge, you can now make decisions and effectively control the flow of your Go programs. Coming up next, we will dive deeper with hands-on practice exercises. So strap in, and let's dive in!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.