There are situations when you want to execute a code based on some condition such as fetching data from server only when connection has been established.
Golang permits this using its if
statement.
if
statement is followed by a condition and code surrounded between curly braces.
This code is executed only when the condition after if evaluates to a bool true. Note that the condition should be such that it should result in either true
or false
, otherwise you get a compiler error.
Syntax to write an
if
statement is given below.
if condition { // conditional code }
It consists of an if
keyword followed by a condition. This condition may be an expression that should result in true
or false
.
Curly braces after if
statement is called if block. There may be any number of statements inside an if
block.
if example
Below is an example of using if
statement to check whether age is greater than 18 or not.
package main import "fmt" func main() { age := 19 if age > 18 { fmt.Println("You are allowed to vote") } }
Logical operators with if
Often there may be multiple conditions to check in a single if statement before executing the code.
Example, before starting a laptop, you might need to test that it is connected to a power source and charging.
With if
, you can add multiple conditions using logical AND and logical OR operators as shown below.
package main import "fmt" func main() { connected := true charging := true if connected && charging { fmt.Println("Laptop is ready to use") } }
Likewise, you can use comparison operators( >
, <
, >=
, <=
, !=
), equality operator( ==
) with along with logical operators with if
statement .
Below code example checks if a number lies between a range(18 to 24),
package main import "fmt" func main() { number := 21 if number >=18 && number <=24 { fmt.Println("Number is betweeen 18 and 24") } }
An
if
statement inside another if
statement is called nested if statement. Inner if
statement is executed only when the outer statement evaluates to true
.
Nested if statements are used to represent a logical AND condition. Thus, above code example may also be written as
package main import "fmt" func main() { number := 21 if number >=18 { // inner if if number <=24 { fmt.Println("Number is betweeen 18 and 24") } } }
There is no limit on the number of levels that you can nest if
statements.
if-else
There are scenarios when you want to execute a code block when the if
statement evaluates to false
.
An example of this case would be checking for a database connection in if
block.
If it is connected, then fetch the data otherwise try to connect again in else
block.
Note that else
block executes only when the if
condition is false
. This means that either if
block is executed or else
, not both at the same time.
Syntax of an if-else
construct in Go is
if condition { // executes when condition evaluates to true } else { // executes when condition evaluates to false }
Example of an if-else
block is
package main import "fmt" func main() { number := 15 if number >= 18 { fmt.Println("Number is greater than equal 18") } else { fmt.Println("Number is lesser than 18") } }
When multiple conditions need to be tested, then
if...else if
construct is used. Both if
and else if
statements are followed by a condition and curly braces with the code to be executed.There may be multiple
else if
blocks after the first if
block.
Syntax of if...else if
construct is
if condition1 { // executes when condition1 is true } else if condition2 { // executes when condition2 is true } else if condition3 { // executes when condition3 is true }
As soon as a condition evaluates to true
, the corresponding block is executed and all others are skipped. Remember that only one block out of all blocks is executed.
Example of if...else if
is given below
package main import "fmt" func main() { fruit := "juicy" if fruit == "pulpy" { fmt.Println("May be apple, papaya or guava") } else if fruit == "juicy" { fmt.Println("May be orange, pomegranate or watermelon ") } }
if…else if…else
This is the same as if...else if
but with a default code block that will execute when none of the conditions evaluates to true
.
The default code block is written as an else
block. This construct should be used when there is one block that should be executed in any case.
Example is given below.
package main import "fmt" func main() { number := -4 if number > 0 { fmt.Println("Number is positive") } else if number < 0 { fmt.Println("Number is negative") } else { fmt.Println("Number is zero") } }
Above code checks if a number is positive, negative or 0.
if
statement checks the number to be greater than 0, else if
statement checks the number to be lesser than 0.
When both the conditions evaluate to false
, this implies that the number will be 0 and hence put in the else
block.
In all the above examples, we defined a variable before if and checked it in the
if
condition. But, it is also possible to define the variable in the same line as if
statement.
Variable definition and condition should be separated by a semi-colon as shown below.
package main import "fmt" func main() { if age := 19; age > 18 { fmt.Println("You are allowed to vote") } }
Though the syntax looks concise but it is difficult to understand for someone reading the code, so it is better to define variables before the if
or if-else
blocks.