Nested if Statements in Go Language
Go Language Conditional Statements
You can embed one or more if or else if statements within an if or else if statement.
Syntax
The syntax for an if...else statement in Go programming language is as follows:
if boolean expression 1 {
/* Executed when boolean expression 1 is true */
if boolean expression 2 {
/* Executed when boolean expression 2 is true */
}
}
You can nest else if...else statements in the same way within an if statement.
Example
Using nested if statements:
Example
package main
import "fmt"
func main() {
/* Define local variables */
var a int = 100
var b int = 200
/* Check the condition */
if a == 100 {
/* Executed when the if condition is true */
if b == 200 {
/* Executed when the if condition is true */
fmt.Printf("The value of a is 100, and the value of b is 200\n");
}
}
fmt.Printf("The value of a is: %d\n", a);
fmt.Printf("The value of b is: %d\n", b);
}
The output of the above code is:
The value of a is 100, and the value of b is 200
The value of a is: 100
The value of b is: 200