Go Conditional Statements
Conditional statements let a program make decisions — executing one block of code or another depending on whether an expression is true or false. They are the foundation of control flow in any language, and Go keeps them deliberately simple.
Understanding Conditions
A condition is any expression that evaluates to a bool: true or false. Conditions are built using comparison and logical operators (covered in detail in the Operators lesson).
Comparison operators:
| Operator | Description | Example |
|---|---|---|
< | Less than | a < b |
<= | Less than or equal to | a <= b |
> | Greater than | a > b |
>= | Greater than or equal to | a >= b |
== | Equal to | a == b |
!= | Not equal to | a != b |
Logical operators:
| Operator | Description | Example |
|---|---|---|
&& | Logical AND | (a > b) && (b < c) |
|| | Logical OR | (a > b) || (b < c) |
! | Logical NOT | !(a == b) |
The if Statement
if runs a block of code only when its condition evaluates to true.
if condition {
// code executed when condition is true
}
Key rules:
- The keyword is always lowercase —
IforIFis a compile error, since Go is case-sensitive. - The condition must be a
boolexpression; Go has no concept of "truthy" non-boolean values like0or an empty string standing in forfalse. - Curly braces
{}are mandatory, even for a single statement. - Parentheses around the condition are optional and, by convention, always omitted.
package main
import "fmt"
func main() {
temperature := 32
if temperature > 30 {
fmt.Println("It's a hot day!")
}
}
Example: Comparing Variables
package main
import "fmt"
func main() {
length := 25
width := 20
if length > width {
fmt.Println("Length is greater than width")
}
}
Example: Using an Expression as the Condition
package main
import "fmt"
func main() {
marks := 72
if marks/2 > 30 {
fmt.Println("You passed the evaluation")
}
}
The if...else Statement
else provides a fallback block that runs when the if condition is false, so your program always has a defined outcome for both cases.
if condition {
// code executed when condition is true
} else {
// code executed when condition is false
}
package main
import "fmt"
func main() {
age := 16
if age >= 18 {
fmt.Println("You are eligible to vote.")
} else {
fmt.Println("You are not eligible to vote yet.")
}
}
Example: Checking Time of Day
package main
import "fmt"
func main() {
hour := 21
if hour < 18 {
fmt.Println("Good day!")
} else {
fmt.Println("Good evening!")
}
}
Since hour is 21, the condition hour < 18 is false, so the else branch runs, printing "Good evening!".
Example: Even or Odd Check
package main
import "fmt"
func main() {
number := 7
if number%2 == 0 {
fmt.Println("The number is even")
} else {
fmt.Println("The number is odd")
}
}
The if...else if...else Ladder
else if lets you test additional conditions in sequence, after the first if fails.
if condition1 {
// executes if condition1 is true
} else if condition2 {
// executes if condition1 is false and condition2 is true
} else {
// executes if none of the above are true
}
How it works:
- Conditions are checked from top to bottom.
- The first condition that evaluates to
truehas its block executed. - As soon as a match is found, every remaining condition is skipped — they are never evaluated.
- The final
elseis optional and acts as a catch-all.
package main
import "fmt"
func main() {
score := 78
if score >= 90 {
fmt.Println("Grade: A")
} else if score >= 75 {
fmt.Println("Grade: B")
} else if score >= 60 {
fmt.Println("Grade: C")
} else {
fmt.Println("Grade: D")
}
}
Example: Order of Conditions Matters
package main
import "fmt"
func main() {
value := 30
if value >= 10 {
fmt.Println("Value is at least 10")
} else if value >= 20 {
fmt.Println("Value is at least 20")
} else {
fmt.Println("Value is less than 10")
}
}
Expected output:
Value is at least 10
Even though value >= 20 is also true, the first matching condition (value >= 10) wins, and evaluation stops there. This is a common source of logic bugs — when writing an else if ladder, always order conditions from most specific to least specific (here, value >= 20 should have come first) so a broader condition doesn't accidentally shadow a narrower one.
Nested if Statements
An if statement can contain another if inside its block — this is called a nested if, and it's useful when a decision only makes sense after another condition has already been satisfied.
if condition1 {
// runs if condition1 is true
if condition2 {
// runs if both condition1 and condition2 are true
}
}
package main
import "fmt"
func main() {
num := 20
if num >= 10 {
fmt.Println("Num is more than 10.")
if num > 15 {
fmt.Println("Num is also more than 15.")
}
} else {
fmt.Println("Num is less than 10.")
}
}
Expected output:
Num is more than 10.
Num is also more than 15.
Best practice: while nesting is sometimes necessary, deeply nested if blocks (three or more levels) tend to be hard to read. Where possible, combine the conditions with && instead — if num >= 10 && num > 15 is often clearer than two nested if statements when the inner check doesn't need its own independent else branch.