Compare values
Compare values
Comparisons produce bool results. == tests equality, != inequality, and <, <=, > and >= compare ordered values. Integers, floating-point numbers of compatible types, and strings support ordering; complex numbers support equality only; booleans support equality but not ordering. Use explicit conversions for incompatible typed numeric operands.
package main
import "fmt"
func main() { stock := 8 needed := 8 fmt.Println(stock == needed, stock >= needed) fmt.Println(stock != needed, stock < needed)}Equal stock meets the minimum requirement: >= includes equality but > does not. These results can be stored in bool variables for use in conditions.
Compare text and boolean values
package main
import "fmt"
func main() { fmt.Println("Ada" == "ada", "10" < "2") fmt.Println(true != false)}String comparison is case-sensitive and lexicographic by bytes, not numeric or locale-aware ordering. The text "10" sorts before "2". Do not confuse = (assignment) with == (comparison).
Try it locally
Compare a score of 75 with a minimum of 70. Print whether it meets the minimum and whether it is exactly equal to the minimum. Expected: true false.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Expressions and Arithmetic 02: Compare values
Check the lesson objective before moving on.
Which comparison includes equality?
What is "10" < "2"?