Comparison Operators - Part 2
Part of the Fundamentals section of Coddy's GO journey — lesson 23 of 109.
Go provides additional comparison operators for comparing numerical values.
Use the greater than operator (>) to check if one value is larger than another:
a := 10
b := 5
result := a > b
fmt.Println(result)This outputs:
trueOther comparison operators include less than (<), greater than or equal to (>=), and less than or equal to (<=):
x := 7
y := 7
fmt.Println(x < y) // false
fmt.Println(x >= y) // true
fmt.Println(x <= y) // trueChallenge
BeginnerIn this challenge, you'll practice using comparison operators in Go. We have defined several variables with different values. Your task is to complete the missing comparison expressions to make the program print the expected output.
You need to use the following comparison operators: < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).
Cheat sheet
Go provides comparison operators for numerical values:
Greater than (>):
a := 10
b := 5
result := a > b // trueOther comparison operators:
<- less than>=- greater than or equal to<=- less than or equal to
x := 7
y := 7
fmt.Println(x < y) // false
fmt.Println(x >= y) // true
fmt.Println(x <= y) // trueTry it yourself
package main
import "fmt"
func main() {
// Some predefined variables
age1 := 25
age2 := 30
age3 := 25
// TODO: Complete these comparison expressions using <, >, <=, or >=
isAge1LessThanAge2 := // Check if age1 is less than age2
isAge2GreaterThanAge1 := // Check if age2 is greater than age1
isAge1LessThanOrEqualToAge3 := // Check if age1 is less than or equal to age3
isAge2GreaterThanOrEqualToAge3 := // Check if age2 is greater than or equal to age3
// Print the results
fmt.Println("Is age1 less than age2?", isAge1LessThanAge2)
fmt.Println("Is age2 greater than age1?", isAge2GreaterThanAge1)
fmt.Println("Is age1 less than or equal to age3?", isAge1LessThanOrEqualToAge3)
fmt.Println("Is age2 greater than or equal to age3?", isAge2GreaterThanOrEqualToAge3)
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions