Concept Flow - Why conditional logic is needed
Start
Check Condition
Yes|No
Do Action A
End
The program starts, checks a condition, then chooses one of two actions based on that condition before ending.
package main import "fmt" func main() { age := 18 if age >= 18 { fmt.Println("You can vote") } else { fmt.Println("You cannot vote") } }
| Step | Variable 'age' | Condition 'age >= 18' | Branch Taken | Output |
|---|---|---|---|---|
| 1 | 18 | 18 >= 18 is True | Yes branch | "You can vote" printed |
| 2 | - | - | End | Program ends |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| age | undefined | 18 | 18 |
Conditional logic lets programs choose actions based on conditions.
Syntax: if condition { action } else { other action }
It helps programs behave differently in different situations.
Without it, programs do the same thing every time.
Use conditions to make decisions in your code.if statements in Go programs?if statement in Go?package main
import "fmt"
func main() {
x := 5
if x > 3 {
fmt.Println("Greater")
} else {
fmt.Println("Smaller")
}
}package main
import "fmt"
func main() {
x := 10
if x = 5 {
fmt.Println("x is 5")
}
}Pass the test and this pattern moves unknown