Conditional constructs in Programming languages allow us to make decision based on certain conditions. Usually every programming language has such kind of constructs using if...else keywords. These usually take a logical statement and based on whether it returns true or false, one of the blocks get executed.
Overview
This lesson talks about conditional statements using simple if construct followed by if..else blocks. We will also take a look at nested if statements and multiple conditions using if..else if..else constructs.
The general syntax for if...else looks like this.
1if conditional {
2 // statements
3} else if condition2 {
4 // statements
5} else {
6 // else statements
7}
If Statements
Let’s first look at flow chart for if block.
flowchart TD;
Start --> C{Condition Check}
C -->|true| D[If block statements]
D --> E[End]
C -->|false| E[End]
In this case, we check the condition and if this condition is true, the If block statements get executed else the flow skips the if block.
1package main
2
3import "fmt"
4
5func main() {
6 num := 10
7 if num > 10 {
8 fmt.Println(num)
9 }
10 fmt.Println("Ending the loop")
11}
Output
Ending the loop
If we modify the num variable with a value of 20. It returns below result.
20
Ending the loop
If…Else Statements
Below is a flow chart for if...else statements in Go. In this case, if conditional check returns true, the if block gets executed otherwise else block gets executed.
flowchart TD
Start --> C{Condition Check}
C -->|true| D[If Block]
D --> F[End]
C -->|false| E[Else Block]
E --> F[End]
Below code checks whether a person can drive or not depending on user’s input.
1package main
2
3import "fmt"
4
5func main() {
6 // Check whether a person is eligible to drive or not
7 var age int
8 fmt.Println("Enter you age: ")
9 fmt.Scanf("%d", &age)
10 if age > 18 {
11 fmt.Println("Congratulations, you can drive")
12 } else {
13 fmt.Printf("You will have to wait %d years before you can drive.\n", 18-age)
14 }
15}
go run main.go
Enter you age:
32
Congratulations, you can drive
If..Else If..Else Statements
This check is interesting. In this case, first it checks condition 1. If this returns true then first if block gets executed. If this returns false, then next we check for condition 2 in else if block. If this condition 2 returns true, else if block gets executed. If this also returns false, then we finally go to else block.
flowchart TD
Start --> C{Condition 1}
C -->|true| D[If Block]
D --> F[End]
Start --> G{Condition 2}
G -->|true| H[Else If Block]
H --> F[End]
C -->|false| E[Else]
G -->|false| E[Else]
E --> F[End]
1package main
2
3import "fmt"
4
5func main() {
6 grades := 75
7 if grades > 80 {
8 fmt.Println("You've passed with A+ grades")
9 } else if grades > 70 {
10 fmt.Println("You've passed with A grades")
11 } else if grades > 55 {
12 fmt.Println("You've passed with B grades.")
13 } else if grades > 35 {
14 fmt.Println("You've passed with C grades.")
15 } else {
16 fmt.Println("Unfortunately, you've failed.")
17 }
18}
You've passed with A grades.
In this case, the sequence of conditionals matter. Whichever condition from top to bottom first becomes true, that block will be executed. Also, with this construct, it will executed only one of the blocks.
Nested If..Else Statements
If we want to check multiple conditions, we can also include nested if..else blocks. This will verify multiple conditions depending on first condition check. In upcoming example, we first verify person’s age. If he is qualified for driving, we also verify whether the person has a valid driving licence by asking for this information. Depending on answers, we provide the output.
1package main
2
3import "fmt"
4
5func main() {
6 // Check if a person is eligible and whether he has the valid driving license.
7
8 var age int
9 var isDrivingLicenceValid string
10 fmt.Println("Enter you age: ")
11 fmt.Scanf("%d", &age)
12 if age > 18 {
13 fmt.Println("Do you have a valid driving licence? y/n")
14 fmt.Scanf("%s", &isDrivingLicenceValid)
15 if isDrivingLicenceValid == "y" || isDrivingLicenceValid == "yes" {
16 fmt.Println("Congratulations, you can drive")
17 } else {
18 fmt.Println("Oh no, you need valid driving licence to drive.")
19 }
20 } else {
21 fmt.Printf("You will have to wait %d years before you can drive.\n", 18-age)
22 }
23}
Below are outputs of two runs with different inputs.
go run main.go
Output
Enter you age:
23
Do you have a valid driving licence? y/n
y
Congratulations, you can drive
Enter you age:
23
Do you have a valid driving licence? y/n
n
Oh no, you need valid driving licence to drive.
Conclusion
In this lesson, we learned about different ways to write if..else statements in Go.


Comments