In this tutorial you will learn about the Go if else Statement and its application with practical example.
Go if Statement
If statement allows a block of code to be executed only when a specified condition is true. An if statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be true or false.
Syntax:-
1 2 3 |
if(condation){ // statement(s) to be executed when condition is true } |
Here, you can omit the parentheses () but the curly braces {} are mandatory.
Example:-
1 2 3 4 5 6 7 8 9 10 |
package main import "fmt" func main() { var x = 25 fmt.Println("W3Adda - Go If Statement") if(x > 10) { fmt.Printf("%d is greater than 10\n", x) } } |
Output:-
Multiple test expressions can also be combined using && (AND) and || (OR) operators as following –
1 2 3 4 |
var age = 24 if age >= 18 && age <= 30 { fmt.Println("I am Young.") } |
Go if…else Statement
When we want to execute some block of code if a condition is true and another block of code if a condition is false, In such a case we use if….else statement.
Syntax:-
1 2 3 4 5 6 |
if (condition) { // statement(s) to be executed when condition is true } else { // statement(s) to be executed when condition is false } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package main import "fmt" func main() { var a = 10 var b = 20 fmt.Println("W3Adda - Go If...else Statement") if (a > b) { fmt.Printf("a is greater than b") } else { fmt.Printf("b is greater than a") } } |
Output:-
Go Ladder if..else..if Statement
The if..else..if Ladder statement is used to select one of among several blocks of code to be executed.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package main import "fmt" func main() { var a = 10 var b = 10 fmt.Println("W3Adda - Go Ladder If...else Statement") if (a > b) { fmt.Printf("a is greater than b") } else if(a == b){ fmt.Printf("a and b are equal") } else{ fmt.Printf("b is greater than a") } } |
Output:-
Go Nested if Expression
When there is an if statement inside another if statement then it is known as nested if else.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package main import "fmt" func main() { var a = 25 fmt.Println("W3Adda - Go Nested If Statement") if (a < 100) { if (a < 50) { fmt.Println("a is less than 50") } if (a >= 50) { fmt.Println("a is greater than 50") } } } |
Output:-
Go If with short statement
In Go, the conditional expression can be preceded by a simple statement, which is executes before the conditional expression is evaluated, and if it is a declaration statement then the variable declared in the statement will only be available inside the if block and it’s else or else-if sections.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package main import "fmt" func main() { fmt.Println("W3Adda - Go If Statement with short statement.") if x := 10; x%2 == 0 { fmt.Printf("%d is even\n", x) } else { fmt.Printf("%d is odd\n", x) } } |
Output:-
Note:- Use of parentheses in case of short statement results in a syntax error.