Skip to content

Commit 816b4e8

Browse files
Tweak concept
1 parent 8b42d70 commit 816b4e8

File tree

4 files changed

+81
-47
lines changed

4 files changed

+81
-47
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"blurb": "TODO: add blurb",
3-
"authors": ["sachinmk27"],
2+
"blurb": "Execute the same piece of code several times",
3+
"authors": [
4+
"sachinmk27",
5+
"andrerfcsantos"
6+
],
47
"contributors": []
5-
}
8+
}

concepts/for-loops/about.md

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,57 @@
1-
# Introduction
1+
# About
22

33
## General syntax
44

5-
The `for` loop is one of the most commonly used statements to repeatedly execute some logic.
6-
The basic `for` loop has three components separated by semicolons:
7-
8-
- the **init statement**: executed once before the first iteration
9-
- the **condition expression**: evaluated before every iteration
10-
- the **post statement**: executed at the end of every iteration
5+
The for loop is one of the most commonly used statements to repeatedly execute some logic. In Go it consists of the `for` keyword, a header and a code block that contains the body of the loop wrapped in curly brackets. The header consists of 3 components separated by semicolons `;`: init, condition and post.
116

127
```go
13-
for initialization; condition; step {
14-
// code that is executed repeatedly as long as the condition is true
8+
for init; condition; post {
9+
// loop body - code that is executed repeatedly as long as the condition is true
1510
}
1611
```
1712

18-
**Note:** Unlike other languages like C, Java, or JavaScript there are no parentheses surrounding the three components of the `for` statement and the braces `{ }` are always required.
13+
- The **init** component is some code that runs only once before the loop starts.
14+
- The **condition** component must be some expression that evaluates to a boolean and controls when the loop should stop. The code inside the loop will run as long as this condition evaluates to true. As soon as this expression evaluates to false, no more iterations of the loop will run.
15+
- The **post** component is some code that will run at the end of each iteration.
1916

20-
## Header
17+
**Note:** Unlike other languages, there are no parentheses `()` surrounding the three components of the header. In fact, inserting such parenthesis is a compilation error. However, the braces `{ }` surrounding the loop body are always required.
2118

22-
The initialization usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the step increments the counter at the end of each repetition.
19+
## For Loops - An example
2320

24-
The individual parts of the header are separated by semicolons.
21+
The init component usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the post component usually increments the counter at the end of each repetition.
2522

2623
```go
27-
28-
for i := 1; i <= 10; i++ {
24+
for i := 1; i < 10; i++ {
2925
fmt.Println(i)
3026
}
3127
```
3228

33-
Defining the step is often done using the increment or decrement statement as shown in the example above.
29+
This loop will print the numbers from `1` to `9` inclusivé.
30+
Defining the step is often done using an increment or decrement statement, as shown in the example above.
3431

35-
Also, the init and post statements are optional.
3632

37-
```go
38-
i := 1
33+
## Optional components of the header
3934

40-
for i <= 3 {
41-
42-
fmt.Println(i)
43-
i = i + 1
44-
45-
}
46-
```
47-
48-
## The "while" loop
49-
50-
At that point you can drop the semicolons: C's `while` is spelled `for` in Go.
35+
The init and post components of the header are optional:
5136

5237
```go
53-
var sum int = 1
38+
var sum = 1
5439
for sum < 1000 {
5540
sum += sum
5641
}
5742
fmt.Println(sum)
5843
// Output: 1024
5944
```
6045

61-
## Break, Continue and Labels
46+
This is similar to a `while` loop in other languages.
47+
48+
In Go, there is no `while` keyword. A `while` loop can be expressed in Go by writing a `for` loop without the init and post parts of the header, like in the example above.
49+
50+
6251

63-
Inside a loop body you can use the `break` keyword to stop the execution of the loop entirely.
52+
## Break and Continue
53+
54+
Inside a loop body you can use the `break` keyword to stop the execution of the loop entirely:
6455

6556
```go
6657
for n := 0; n <= 5; n++ {
@@ -75,7 +66,7 @@ for n := 0; n <= 5; n++ {
7566
// 2
7667
```
7768

78-
In contrast, the keyword `continue` only stops the execution of the current iteration and continues with the next one.
69+
In contrast, the keyword `continue` only stops the execution of the current iteration and continues with the next one:
7970

8071
```go
8172
for n := 0; n <= 5; n++ {
@@ -92,10 +83,36 @@ for n := 0; n <= 5; n++ {
9283

9384
## Infinite for loop
9485

95-
If you forget or omit the loop condition it loops forever.
86+
The condition part of the loop header is also optional. In fact, you can write a loop with no header:
9687

9788
```go
9889
for {
9990
// Endless loop...
10091
}
10192
```
93+
94+
This loop will never end and will only ever finish if the program exits or has a `break` in its body.
95+
96+
## Labels and goto
97+
98+
When we use `break`, Go will stop running the most inner loop. Similarly, when we use `continue`, Go will run the next iteration of the most inner loop.
99+
100+
However, this is not always desirable. We can use labels together with `break` and `continue` to specifcy exactly from which loop we want to exit or continue, respectively.
101+
102+
In this example we are creating a label `OuterLoop`, that will refer to the most outter loop. In the most inner loop, to say that we want exit from the most outter loop, we then use `break` followed by the name of the label of the most outter loop:
103+
104+
```go
105+
OuterLoop:
106+
for i := 0; i < 10; i++ {
107+
for j := 0; j < 10; j++ {
108+
// ...
109+
break OuterLoop
110+
}
111+
}
112+
```
113+
114+
Using labels with `continue` would also work, in which case, Go would continue in the next iteration of the loop referenced by the label.
115+
116+
Go also has a `goto` keyword that works in a similar way and allows us to jump to from a piece of code to another labeled piece of code.
117+
118+
**Warning:** Even though Go allows to jump to a piece of code marked with a label with `continue`, `break` or `goto`, using this feature of the language can easily make the code very hard to read. For this reason, using labels is often not recommended.

concepts/for-loops/introduction.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
# Introduction
12
## General syntax
23

3-
The for loop is one of the most commonly used statements to repeatedly execute some logic. The basic `for` loop has three components separated by semicolons:
4-
5-
- the **init statement**: executed before the first iteration
6-
- the **condition expression**: evaluated before every iteration
7-
- the **post statement**: executed at the end of every iteration
4+
The for loop is one of the most commonly used statements to repeatedly execute some logic. In Go it consists of the `for` keyword, a header and a code block that contains the body of the loop wrapped in curly brackets. The header consists of 3 components separated by semicolons `;`: initilization, condition and step.
85

96
```go
107
for initialization; condition; step {
11-
// code that is executed repeatedly as long as the condition is true
8+
// loop body - code that is executed repeatedly as long as the condition is true
129
}
1310
```
1411

15-
**Note:** Unlike other languages like C, Java, or JavaScript there are no parentheses surrounding the three components of the `for` statement and the braces `{ }` are always required.
12+
- The **initialization** component is some code that runs only once before the loop starts.
13+
- The **condition** component must be some expression that evaluates to a boolean and controls when the loop should stop. The code inside the loop will run as long as this condition evaluates to true. As soon as this expression evaluates to false, no more iterations of the loop will run.
14+
- The **step** component is some code that will run at the end of each iteration.
15+
16+
**Note:** Unlike other languages, there are no parentheses `()` surrounding the three components of the header. In fact, inserting such parenthesis is a compilation error. However, the braces `{ }` surrounding the loop body are always required.
1617

17-
## Header
18+
## For Loops - An example
1819

1920
The initialization usually sets up a counter variable, the condition checks whether the loop should be continued or stopped and the step increments the counter at the end of each repetition.
2021

concepts/for-loops/links.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
[]
1+
[
2+
{
3+
"url": "https://tour.golang.org/flowcontrol/1",
4+
"description": "Tour of Go: For"
5+
},
6+
{
7+
"url": "https://gobyexample.com/for",
8+
"description": "Go by Example: For"
9+
},
10+
{
11+
"url": "https://yourbasic.org/golang/for-loop/",
12+
"description": "Article: 5 basic for loop patterns"
13+
}
14+
]

0 commit comments

Comments
 (0)