|
| 1 | +Tutorial |
| 2 | +-------- |
| 3 | + |
| 4 | +In Golang we have if-else statements to check a condition and execute the relevant code. |
| 5 | + |
| 6 | +## if statement |
| 7 | + |
| 8 | +The if statement is used to check if a condition is met and execute the code inside the if block only if the condition is met. |
| 9 | + |
| 10 | +The general syntax is |
| 11 | + |
| 12 | + if <condition> { |
| 13 | + // Code to be executed if the condition is met. |
| 14 | + } |
| 15 | + |
| 16 | +Let us write a example code to test if we need to take an umbrella based on if it's raining or not outside. |
| 17 | + |
| 18 | + isRaining := true |
| 19 | + |
| 20 | + if isRaining { |
| 21 | + fmt.Println("It's raining, take an umbrella") |
| 22 | + } |
| 23 | + |
| 24 | +## else statement |
| 25 | + |
| 26 | +If the condition in a if statement is not met then we execute the else block if it is defined. |
| 27 | + |
| 28 | +The syntax is |
| 29 | + |
| 30 | + if <condition> { |
| 31 | + // code executes if condition is true |
| 32 | + } else { |
| 33 | + // code executes if condition is false |
| 34 | + } |
| 35 | + |
| 36 | +One thing to remember here is the else statement should always be in the same line as the closing `}` of the if statement. |
| 37 | + |
| 38 | + // below code doesn't work as else is not in the same line as if's closing } |
| 39 | + if <condition> { |
| 40 | + // code executes if condition is true |
| 41 | + } |
| 42 | + else { |
| 43 | + // code executes if condition is false |
| 44 | + } |
| 45 | + |
| 46 | +In the last module, we checked if it's raining and we gave a message if it is raining. But our code doesn't print anything if it is not raining. Let us fix it by adding an else statement and providing more information. |
| 47 | + |
| 48 | + isRaining := true |
| 49 | + |
| 50 | + if isRaining { |
| 51 | + fmt.Println("It's raining, take an umbrella") |
| 52 | + } else { |
| 53 | + fmt.Println("It's not raining. Umbrella not needed") |
| 54 | + } |
| 55 | + |
| 56 | +## Example |
| 57 | +Let's write an example code to check if the user's name is `John` or not |
| 58 | + |
| 59 | + userName := "John" |
| 60 | + |
| 61 | + // prints You are John |
| 62 | + if userName == "John" { |
| 63 | + fmt.Println("You are John") |
| 64 | + } else { |
| 65 | + fmt.Println("You are not John") |
| 66 | + } |
| 67 | + |
| 68 | + // let's try again changing the username variable |
| 69 | + userName = "Mathew" |
| 70 | + |
| 71 | + // prints You are not John |
| 72 | + if userName == "John" { |
| 73 | + fmt.Println("You are John") |
| 74 | + } else { |
| 75 | + fmt.Println("You are not John") |
| 76 | + } |
| 77 | + |
| 78 | +You can cascade if statement in an else statement to check for more conditions and run the relevant code. |
| 79 | + |
| 80 | +Now let's write some code to check if a user age is below 20 or between 20 and 60 or above 60. |
| 81 | + |
| 82 | + userAge = 26 |
| 83 | + |
| 84 | + if userAge < 20 { |
| 85 | + fmt.Println("Below 20") |
| 86 | + } else if userAge >= 20 && userAge <= 60 { |
| 87 | + fmt.Println("Between 20 and 60") |
| 88 | + } else { |
| 89 | + fmt.Println("Above 60") |
| 90 | + } |
| 91 | + |
| 92 | +The if statement also provides an option to initialize a variable and test the condition within the if statement. The general syntax in this case is |
| 93 | + |
| 94 | + if <init>;<condition> { |
| 95 | + // code to execute if condition is true |
| 96 | + } |
| 97 | + |
| 98 | +An example code is given below. |
| 99 | + |
| 100 | + if userName := "Jeremy"; userName == "John" { |
| 101 | + fmt.Println("You are John") |
| 102 | + } else { |
| 103 | + fmt.Println("You are not John") |
| 104 | + } |
| 105 | + |
| 106 | +Exercise |
| 107 | +-------- |
| 108 | +In a country, a person is allowed to vote if his/her age is above or equal to 18. Check the userAge variable and print `"Eligible"` if the person is eligible to vote or `"Not Eligible"` if the person is not eligible. |
| 109 | + |
| 110 | +Tutorial Code |
| 111 | +------------- |
| 112 | +package main |
| 113 | + |
| 114 | +import "fmt" |
| 115 | + |
| 116 | +func main () { |
| 117 | + userAge := 22 |
| 118 | + |
| 119 | + // Add your code here. |
| 120 | +} |
| 121 | + |
| 122 | +Expected Output |
| 123 | +--------------- |
| 124 | +Eligible |
| 125 | + |
| 126 | +Solution |
| 127 | +-------- |
| 128 | +package main |
| 129 | + |
| 130 | +import "fmt" |
| 131 | + |
| 132 | +func main () { |
| 133 | + userAge := 22 |
| 134 | + |
| 135 | + if userAge >= 18 { |
| 136 | + fmt.Println("Eligible") |
| 137 | + } else { |
| 138 | + fmt.Println("Not Eligible") |
| 139 | + } |
| 140 | +} |
0 commit comments