Skip to content

Commit b1e4e86

Browse files
committed
Add information about conditional statements
1 parent a265e85 commit b1e4e86

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ I believe to get a good understanding, you can follow this order:
3636
- [Constants & variables](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/constants_and_variables.go)
3737
- [Stdin & stdout](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/stdin_stdout.go)
3838
- [Arrays](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/array.go)
39-
- [Conditional structures]()
39+
- [Conditional statements](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/conditional_statements.go)
4040
- [Loops]()
4141
- [Pointers](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/pointers.go)
4242
- [Structs](https://github.com/mfbmina/data-structures-algorithms-go/blob/main/structs.go)

conditional_statements.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// Conditionals help us to choose an action based on the data that we have.
6+
//
7+
// To have conditional statements, we need to have our conditional operators.
8+
// Conditional operators tell you which comparisons you are allowed to do.
9+
// In Golang, we have the following operators:
10+
// - `A == B`: returns `true` if A is equal to B
11+
// - `A != B`: returns `true` if A is different from B
12+
// - `A > B`: returns `true` if A is higher than B
13+
// - `A >= B`: returns `true` if A is higher or equal to B
14+
// - `A < B`: returns `true` if A is lower than B
15+
// - `A <= B`: returns `true` if A is lower or equal to B
16+
//
17+
// We also have logical operators:
18+
// - `X && Y`: `&&` is the logical operator `AND`. It expects both expressions to be true.
19+
// - `X || Y`: `||` is the logical operator `OR`. It expects one expression to be true.
20+
// - `!X`: `!` is the operator `NOT`. It negates the value of the expression. If the expression is already false, it becomes true. `!false == true`.
21+
//
22+
// In Golang we have two ways of doing conditional statements: `if/else` and `switch/case`.
23+
func main() {
24+
var age int
25+
var country string
26+
fmt.Println("How old are you?")
27+
fmt.Scanln(&age)
28+
fmt.Println("Where are you from?")
29+
fmt.Scanln(&country)
30+
31+
// One good example is asking your age before visiting some websites.
32+
// If the person is a minor, we shouldn't allow him/her to visit the page.
33+
// This case is what we call `if/else` statement because it should choose between actions by evaluating expressions as true or false.
34+
// It will always execute the first true condition and if no condition is satisfied, it will execute the else.
35+
if age >= 21 {
36+
// If the provided age is equal or higher to 21, it will print "Welcome!"
37+
fmt.Println("Welcome!")
38+
} else if age >= 18 && country != "US" {
39+
// If the provided age is equal or higher to 18 and the user is not from the US it will print the message.
40+
fmt.Println("Quite young but still welcome!")
41+
} else {
42+
// If the age is below 18 it will print the following message.
43+
fmt.Println("hmmm... maybe you should not be allowed on here!")
44+
}
45+
46+
var animal string
47+
fmt.Println("What is your favorite animal?")
48+
fmt.Scanln(&animal)
49+
50+
// Sometimes, we do not want to check if something is true or false, but to check if it has a specific value.
51+
// This is the best option to use the switch statement.
52+
switch animal {
53+
case "dog":
54+
fmt.Println("Dogs bark, right?")
55+
case "cat":
56+
fmt.Println("Meow!")
57+
case "fish":
58+
fmt.Println("I've no clue how a fish sounds...")
59+
default:
60+
fmt.Println("Hm... I need to upgrady my animal list...")
61+
}
62+
}

0 commit comments

Comments
 (0)