Skip to content

Commit c905869

Browse files
Tweak concept
1 parent 8b42d70 commit c905869

File tree

5 files changed

+92
-65
lines changed

5 files changed

+92
-65
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: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,54 @@
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.
36-
37-
```go
38-
i := 1
32+
## Optional components of the header
3933

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.
34+
The init and post components of the header are optional:
5135

5236
```go
53-
var sum int = 1
37+
var sum = 1
5438
for sum < 1000 {
5539
sum += sum
5640
}
5741
fmt.Println(sum)
5842
// Output: 1024
5943
```
6044

61-
## Break, Continue and Labels
45+
This is similar to a `while` loop in other languages.
46+
47+
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.
48+
49+
## Break and Continue
6250

63-
Inside a loop body you can use the `break` keyword to stop the execution of the loop entirely.
51+
Inside a loop body you can use the `break` keyword to stop the execution of the loop entirely:
6452

6553
```go
6654
for n := 0; n <= 5; n++ {
@@ -75,7 +63,7 @@ for n := 0; n <= 5; n++ {
7563
// 2
7664
```
7765

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

8068
```go
8169
for n := 0; n <= 5; n++ {
@@ -89,13 +77,38 @@ for n := 0; n <= 5; n++ {
8977
// 3
9078
// 5
9179
```
92-
9380
## Infinite for loop
9481

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

9784
```go
9885
for {
9986
// Endless loop...
10087
}
10188
```
89+
90+
This loop will never end and will only ever finish if the program exits or has a `break` in its body.
91+
92+
## Labels and goto
93+
94+
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.
95+
96+
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.
97+
98+
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:
99+
100+
```go
101+
OuterLoop:
102+
for i := 0; i < 10; i++ {
103+
for j := 0; j < 10; j++ {
104+
// ...
105+
break OuterLoop
106+
}
107+
}
108+
```
109+
110+
Using labels with `continue` would also work, in which case, Go would continue in the next iteration of the loop referenced by the label.
111+
112+
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.
113+
114+
**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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
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.
1615

17-
## Header
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.
1817

19-
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.
18+
## For Loops - An example
2019

21-
The individual parts of the header are separated by semicolons.
20+
The initialization component 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.
2221

2322
```go
2423
for i := 1; i < 10; i++ {
2524
fmt.Println(i)
2625
}
2726
```
2827

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

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+
]

exercises/concept/bird-watcher/.docs/introduction.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,30 @@
22

33
## General syntax
44

5-
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:
6-
7-
- the **init statement**: executed before the first iteration
8-
- the **condition expression**: evaluated before every iteration
9-
- 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 `;`: initilization, condition and step.
106

117
```go
128
for initialization; condition; step {
13-
// code that is executed repeatedly as long as the condition is true
9+
// loop body - code that is executed repeatedly as long as the condition is true
1410
}
1511
```
1612

17-
**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 **initialization** 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 **step** component is some code that will run at the end of each iteration.
1816

19-
## 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.
2018

21-
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
2220

23-
The individual parts of the header are separated by semicolons.
21+
The initialization component 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.
2422

2523
```go
26-
var list []int = []int{1,2,3}
27-
28-
for i := 1; i < len(list); i++ {
24+
for i := 1; i < 10; i++ {
2925
fmt.Println(i)
3026
}
3127
```
3228

33-
Defining the step is often done using an 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.
31+

0 commit comments

Comments
 (0)