Every If statement in Go programming contains a condition or expression followed by a list of code lines. If the condition result is true, then that blocks of code will run. And if the condition results false, it will exit from the if statement. The If statement Syntax
if expression { block1 }
Go If Statement Example
package main import "fmt" func main() { fmt.Print("Please enter any Integer = ") var num int fmt.Scanf("%d", &num) fmt.Println(num) if num%2 == 0 { fmt.Println("Even Number") } }
In the above example, it checks whether the user-entered value is divisible by 2. If true, it prints the even number. What if you want to print the odd number or write some statements when the condition fails. In such instances, you can use the If Else statements.
Go If Else Condition
If the condition is True, GC will execute code inside the if block; otherwise, it runs the else code block. The Syntax of the If Else statement is
if expression { block1 } else { block2 }
Golang If Else Example
package main import "fmt" func main() { fmt.Print("Please enter any Integer = ") var num int fmt.Scanf("%d", &num) fmt.Println(num) if num%2 == 0 { fmt.Println(num, " is an Even Number") } else { fmt.Println(num, "is an Odd Number") } }
Go Else If
In real-time, there are some situations where we have to check multiple conditions to return the output. For this, Go programming supports Else If statements, which are helpful to check various conditions. The Else if statement will check the condition from top to bottom. If the first condition is true, it will execute that code block and exit. If multiple conditions are correct, it will only run the first one and never go beyond that line of code. The syntax of the Else If statement is
if expression1 { block1 } else if expression2 { block2 } else if expression3 { block3 } else { block4 }
In the below example, if the user enters 520, it will execute the first block though it passes the else if condition, it will never run.
package main import "fmt" func main() { fmt.Print("Please enter the Marks (Max = 600) = ") var marks int fmt.Scanf("%d", &marks) if marks >= 500 { fmt.Println("Eligible for 10% Scholorship") } else if marks >= 550 { fmt.Println("Eligible for 20% Scholorship") } else if marks >= 575 { fmt.Println("Congratulations") fmt.Println("Full Schoilorship") } else { fmt.Println("Not Eligible for Scholorship") fmt.Println("We are Sorry") } }
Go Nested If
Go programming also supports the nested if statements (if statement inside another if condition). In the below example, we nested if statement within the else blocks. If the age is above 18, it further checks other the age is between 18 and 62. If both the conditions, i.e., (age > 18), and (age >= 18 && age <= 62) the code inside this nested if block will execute. It means Nested if statements help us to check the conditions further.
package main import "fmt" func main() { fmt.Print("Please enter the Age = ") var age int fmt.Scanf("%d", &age) if age < 18 { fmt.Println("You are Minor. Not eligible to work") } else { if age >= 18 && age <= 62 { fmt.Println("You can Apply for this position") fmt.Println("Please fill your details") } else { fmt.Println("As per Gov, you are permitted to work") } } }