Decision Making in Go
Decision Making: Decisions in a Go program are used when the program must choose between multiple paths of execution depending on certain conditions.
Types of Decision Making Statements in Go
ifstatementif-elsestatementnested ifstatementif-else ladderstatementswitchstatement (Expression and Type Switch)selectstatement
1. if Statement
The if statement executes a block of code only when the given condition evaluates to true.
Syntax:
if expression {
// execute statements
}
Example Program:
package main
import "fmt"
func main() {
var a int = 50
if (a < 500) {
fmt.Printf("a is less than 500\n")
}
fmt.Printf("Value of a is : %d\n", a)
}
Output:
a is less than 500
Value of a is : 50
2. if else Statement
The if-else statement executes one block if the condition is true, and another block if it’s false.
Syntax:
if expression {
// execute statements
} else {
// execute statements
}
Example Program:
package main
import "fmt"
func main() {
var a int = 50
if (a < 500) {
fmt.Printf("a is less than 500\n")
} else {
fmt.Printf("a is greater than 500\n")
}
}
Output:
a is less than 500
3. Nested if Statement
A nested if statement means one if inside another.
This is used when a decision depends on multiple conditions.
Syntax:
if (FirstCondition) {
// Execute code when FirstCondition is true
if (SecondCondition) {
// Execute code when SecondCondition is true
}
}
Example Program:
package main
import "fmt"
func main() {
var a int = 20
var b int = 20
if (a == 20) {
if (b == 20) {
fmt.Printf("Value of a and b is 20\n")
}
}
}
Output:
Value of a and b is 20
4. if else ladder Statement
The if-else ladder allows testing multiple conditions sequentially.
As soon as one condition is true, the corresponding block executes and the rest are skipped.
Syntax:
if (FirstCondition) {
// Execute code when FirstCondition is true
} else if (SecondCondition) {
// Execute code when SecondCondition is true
} else if (ThirdCondition) {
// Execute code when ThirdCondition is true
} else {
// Execute code when none of the conditions are true
}
Example Program:
package main
import "fmt"
func main() {
var a int = 20
var b int = 20
if (a > b) {
fmt.Printf("a is greater\n")
} else if (a == b) {
fmt.Printf("both are equal\n")
} else {
fmt.Printf("b is greater\n")
}
}
Output:
both are equal
5. Switch Statement
A switch statement allows comparing a value against multiple cases.
It supports both expression switch and type switch in Go.
Expression Switch:
package main
import "fmt"
func main() {
var grade string = "B"
switch grade {
case "A":
fmt.Println("Excellent!")
case "B":
fmt.Println("Good job!")
case "C":
fmt.Println("Well done")
default:
fmt.Println("Keep improving!")
}
}
Output:
Good job!
Type Switch:
Used to check the type of an interface variable.
package main
import "fmt"
func main() {
var value interface{} = 42
switch v := value.(type) {
case int:
fmt.Println("Integer:", v)
case string:
fmt.Println("String:", v)
default:
fmt.Println("Unknown type")
}
}
Output:
Integer: 42
6. Select Statement
The select statement is used for channel-based communication in Go concurrency.
It waits for communication on multiple channels and executes the case that becomes ready first.
Example Program:
package main
import "fmt"
func main() {
ch1 := make(chan string)
ch2 := make(chan string)
go func() { ch1 <- "from channel 1" }()
go func() { ch2 <- "from channel 2" }()
select {
case msg1 := <-ch1:
fmt.Println(msg1)
case msg2 := <-ch2:
fmt.Println(msg2)
default:
fmt.Println("No communication yet")
}
}
Output:
from channel 1
(or)
from channel 2
(depending on which goroutine executes first)
Summary:
Decision-making statements in Go provide structured ways to handle conditions, enabling control over program flow. From simple if checks to concurrent select statements, Go ensures flexible and efficient logic control.