Skip to content

Commit 3f5e5e8

Browse files
shrn01ronreiter
authored andcommitted
Added if-else, more info in Arrays.md, Loops.md
Added tutorial for if-else statements in golang. Added some information in Arrays.md Added some information and using for loop as a while loop in Loops.md
1 parent c221390 commit 3f5e5e8

File tree

4 files changed

+171
-5
lines changed

4 files changed

+171
-5
lines changed

tutorials/learn-golang.org/en/Arrays.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ Arrays
44
------
55
Arrays are essentially storage spaces that can be filled with as much data as one would like. Variables, unlike arrays, can only contain one piece of data. Now there are some caveats. For instance, an array is syntactically created using one data type, just like variables. Yet, an array grants ease of access and far more capabilities when considering large/vast amounts of data compared to a variable(single storage space/value).
66

7+
A array in golang has fixed size similar to C and C++ that we give while we define it. We can't change the size of the array dynamically while the program is running.
8+
79
Examples
810
--------
911

12+
Arrays in golang are defined using the syntax,
13+
14+
var <name of array> [<size of array>]<type of data stored in the array>
15+
16+
Arrays in golang are 0 indexed i.e. the index starts from 0. Let's make some arrays using the syntax given above
17+
1018
// An array named favNums filled with 3 integers
11-
var favNums[3] int
19+
var favNums [3]int
1220

1321
// Insert data into the array
14-
// The first storage space will be assigned the value of 1.
22+
// The first storage space will be assigned the value of 1. It has an index of 0.
1523
favNums[0] = 1
16-
// The second storage space will be assigned the value of 2.
24+
// The second storage space will be assigned the value of 2. It has an index of 1.
1725
favNums[1] = 2
18-
// The third and final storage space will be assigned the value of 3.
26+
// The third and final storage space will be assigned the value of 3. It has an index of 2.
1927
favNums[2] = 3
2028

2129

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}

tutorials/learn-golang.org/en/Loops.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ The loop will stop iterating once the boolean condition evaluates to false.
1919
Examples
2020
--------
2121

22+
The following code prints numbers from 1 to 9.
23+
2224
package main
2325

2426
import "fmt"
@@ -29,10 +31,25 @@ Examples
2931
}
3032
}
3133

34+
You can omit the init and post statement in the `for` loop to get a while loop. The below loop works like a while loop
35+
36+
package main
37+
38+
import "fmt"
39+
40+
func main() {
41+
i := 0
42+
43+
for i < 10 {
44+
fmt.Println(i)
45+
i++
46+
}
47+
}
48+
3249

3350
Exercise
3451
--------
35-
Find the sum of numbers from 1 to 100
52+
Find the sum of numbers from 1 to 100 using a for loop
3653

3754
Tutorial Code
3855
-------------

tutorials/learn-golang.org/en/Welcome.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ learn-golang.org is still under construction - If you wish to contribute tutoria
1313
- [[Hello, World!]]
1414
- [[Variables]]
1515
- [[Arrays]]
16+
- [[If-Else]]
1617
- [[Loops]]
1718

1819
### Contributing Tutorials

0 commit comments

Comments
 (0)