Go If-else Statements
The if statement in Go lang is a decision-making statement that controls the execution flow of the program based on the boolean condition. Here, you will learn about if, elseif, else, and nested if-else statements in Go lang.
if Statement
The if statement contains a boolean condition followed by a single or multi-line code block to be executed. At runtime, if a boolean condition evaluates to true, then the code block will be executed, otherwise not.
if(condition) {
//code block to be executed when if condition evaluates to true
}
The following example demonstrates the if statements.
package main
import "fmt"
func main() {
x := 8
y := 10
if (x < y) {
fmt.Println( "x is less than y")
}
if (x > y) {
fmt.Println( "x is greater than y")
}
}
x is less than y
Above, the first if statement will be executed because the condition x < y
evalutes to true. The code block of the second if statement will not be executed because the condition x > y
evalutes to false.
Note that the condition expression must return a boolean value, true or false; otherwise compiler will give an error.
package main
import "fmt"
func main() {
x := 8
y := 10
if (x + y) {
fmt.Println( "x is less than y")
}
}
# example
./prog.go:9:8: non-boolean condition in if statement
The starting code block brace {
must be specified after the if clause. Go compiler will throw an error if specified in the new line after the if clause.
package main
import "fmt"
func main() {
x := 8
y := 10
if (x < y) { // correct
fmt.Println( "x is less than y")
}
if (x > y)
{ //error
fmt.Println( "x is greater than y")
}
}
The boolean condition can be specified without the parentheses ( )
but the braces { }
are required, as shown below.
package main
import "fmt"
func main() {
x := 8
y := 10
if x < y {
fmt.Println( "x is less than y")
}
if x > y {
fmt.Println( "x is greater than y")
}
}
Like for loop, the if statement can start with a statement to execute before the condition where you can declare a variable whose scope would be limited to the code block until the end of the if.
package main
import "fmt"
func main() {
x := 8
if y := 10; x < y {
fmt.Println( "x is less than y")
}
}
x is less than y
else Clause
Use the else clause after the if statement to execute the code block when the if condition evaluates to false. The else clause must come after the if block. Only one else block is allowed after the if statement.
if condition {
// executed when the if condition is true
} else {
// executed when the if condition is false
}
The following demonstrates the if else statement.
package main
import "fmt"
func main() {
x := 8
y := 10
if ( x < y) {
fmt.Println( "x is less than y")
} else {
fmt.Println("x is greater than or equal to y")
}
}
x is less than y
The else keyword must come after the closing brace }
; otherwise the compiler will give an error.
package main
import "fmt"
func main() {
x := 8
y := 10
if ( x < y) {
fmt.Println( "x is less than y")
}
else { //error
Fmt.Println("x is greater than or equal to y")
}
}
Although, the starting brace of else clause can be specified in the new line.
package main
import "fmt"
func main() {
x := 8
y := 10
if ( x < y) {
fmt.Println( "x is less than y")
} else
{ // valid
fmt.Println("x is greater than or equal to y")
}
}
else if statement
Use if clause after the else clause to execute a code block based on a condition when the first if condition evalutes to false. You can use multiple else-if statements between the if clause and else clause.
package main
import "fmt"
func main() {
temp := 20
if (temp < 15) {
fmt.Println("Cold")
} else if (temp < 30) {
fmt.Println("Warm")
} else {
fmt.Println("Hot")
}
}
Warm
In the above example, temp < 15
is false, so the compiler will check next else if
condition temp < 30
, which is true. So, it will execute the else if
code block only.
You can have multiple else-if blocks where only one else-if block will be executed whose condition evalutes to true.
package main
import "fmt"
func main() {
temp := 35
if (temp < 15) {
fmt.Println("Cold")
} else if (temp < 30) {
fmt.Println("Moderate")
} else if (temp < 40) {
fmt.Println("Warm")
} else {
fmt.Println("Hot")
}
}
Warm
Nested if Statements
Go supports if else
statements inside another if
, else
, or else if
code blocks.
The nested if
statements make the code more readable.
if(condition1){ if(condition2){ // code block to be executed when // condition1 and condition2 evaluates to true } else if(condition3) { if(condition4) { // code block to be executed when // only condition1, condition3, and condition4 evaluates to true } else if(condition5) { // code block to be executed when // only condition1, condition3, and condition5 evaluates to true } else { // code block to be executed when // condition1, and condition3 evaluates to true // condition4 and condition5 evaluates to false } } }
The following example demonstrates the nested if condition.
package main
import "fmt"
func main() {
x := 8
y := 10
if (x != y) {
if (x < y) {
fmt.Println("x is less than y");
} else if (x > y) {
fmt.Println("x is greater than y");
}
} else {
fmt.Println("x is equal to y");
}
}
x is less than y