Condition selection is a mechanism to execute certain code based on a specified condition. There are many condition selection types in Go including selection with if
, selection with if else
, selection with if else if
and selection with switch case
.
When using condition selection, the logic operator is used. There are two operators that can be used including AND (&&
) and OR (||
).
||
)This operator returns true if one or two conditions are true.
Condition | Condition | Result |
---|---|---|
False | False | False |
False | True | True |
True | False | True |
True | True | True |
&&
)This operator returns true if two conditions are true.
Condition | Condition | Result |
---|---|---|
False | False | False |
False | True | False |
True | False | False |
True | True | True |
The comparation operator can be used together with logic operator if needed. This is the list of comparation operators that can be used in Go.
Operator | Description |
---|---|
== |
Equals |
!= |
Not equals |
> |
Greater than |
>= |
Greater than or equals |
< |
Less than |
<= |
Less than or equals |
if
This is the basic syntax of condition selection using if
. If the condition is suitable or fulfilled then the code inside if
block is executed.
if condition { // code.. } |
In this example, the condition selection is used to check if a number is an odd number.
package main import ( "fmt" ) func main() { var num int32 = 25 if num%2 != 0 { fmt.Println("Odd number") } } |
Output
Odd number
Based on the code above, the condition selection using if
is used to check if a number is an odd number. Because the result of num % 2
is not equals zero then the condition is fulfilled so the code inside if
block is executed.
if else
This is the basic syntax of condition selection using if else
. If the condition is fulfilled then the code inside if
block is executed. Otherwise, if the condition is not fulfilled then the code inside else
block is executed.
if condition { // code.. } else { // code.. } |
In this example, the condition using if else
is used to check if an username is a valid username.
package main
import ( "fmt" ) func main() { var username string = "firstname" // perform validation with if else if len(username) >= 6 && username != "" { fmt.Println("Welcome, ", username) } else { fmt.Println("Username is invalid") } } |
Output
Welcome, firstname
Based on the code above, the if else
condition selection is used to check if an username is valid username. The valid condition is if the length of username is greater than or equals 6 and the username is not empty. Because the condition is fulfilled then the code inside if
block is executed.
If the condition is not fulfilled, the code inside else
block is executed.
package main import ( "fmt" ) func main() { var username string = "hi" // perform validation with if else if len(username) >= 6 && username != "" { fmt.Println("Welcome, ", username) } else { fmt.Println("Username is invalid") } } |
Output
Username is invalid
if else if
This is the basic syntax of condition selection using if else if
.
if condition { // code.. } else if condition { // code.. } else { // code.. } |
In this example, the if else if
condition selection is used to define a grade in alphabet. The grade criteria is defined in this table.
Score | Grade |
---|---|
0-50 | D |
51-70 | C |
71-79 | B |
80-100 | A |
package main import ( "fmt" ) func main() { var score int = 78 if score >= 80 && score <= 100 { fmt.Println("A") } else if score >= 71 && score <= 79 { fmt.Println("B") } else if score >= 51 && score <= 70 { fmt.Println("C") } else if score <= 50 { fmt.Println("D") } else { fmt.Println("Invalid score!") } } |
Output
B
Based on the code above, the code is executed based on the condition that fulfilled.
switch case
This is the basic syntax of condition selection with switch case
. The code is executed based on a fulfilled condition in case
. If all the conditions in case
is not fulfilled then the code inside default
is executed.
switch expression { case condition: // code.. .. default: // code.. } |
In this example, the condition selection with switch case
is used.
package main import "fmt" func main() { var job string = "Back End Developer" switch job { case "Front End Developer": fmt.Println("Focus on UI and UX") case "Back End Developer": fmt.Println("Focus on logic") case "Mobile Apps Developer": fmt.Println("Focus on mobile app development") default: fmt.Println("Just do it!") } } |
Output
Focus on logic
The nested condition selection is also available in Go. In this example, the nested condition selection is used with if
selection.
package main import "fmt" func main() { var username string = "admin" var password string = "secret" if username == "admin" { if password == "secret" { fmt.Println("Welcome admin!") } else { fmt.Println("Password is invalid!") } } else { fmt.Println("Username is invalid!") } } |
Output
Welcome admin!
Based on the code above, the outer condition selection is executed then the inner selection is executed.
Use if
selections including if
, if else
or if else if
for complex condition like range condition.
Use switch case
selection for simple condition.
I hope this article is helpful to learn condition selection in Go Programming Language.
Article Contributed By :
|
|
|
|
483 Views |