Skip to content

Commit 7be9222

Browse files
committed
Book Updates
-add chapter 3.2.1
1 parent dcc7e37 commit 7be9222

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

.vitepress/config.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export default defineConfig({
5353
items: [
5454
{
5555
text: "Chapter 3.2.1",
56-
link: "/book/chapter3.3",
56+
link: "/book/chapter3.2.1",
5757
},
5858
],
5959
},

book/chapter3.2.1.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
title: "Chapter 3.2.1"
3+
---
4+
5+
# Chapter 3.2.1: Next and Leave
6+
7+
In the last chapter, we explored `walk` and `while` loops and how to create them. GLang gives you further control of these loops with the `next` and `leave` keywords.
8+
9+
## `next`
10+
11+
If you want to skip to the next part of a loop (next iteration), you can use the `next` keyword anywhere in a `walk` or `while` loop.
12+
13+
```
14+
# walk
15+
walk i = 0 through 10 {
16+
if i == 5 {
17+
next;
18+
}
19+
20+
bark(i); # outputs 0, 1, 2, 3, 4, 6, 7, 8, 9 (skipping 5)
21+
}
22+
23+
# while
24+
obj x = 0;
25+
26+
while x != 10 {
27+
if x == 4 {
28+
next;
29+
}
30+
31+
x = x + 1; # x is incremented as 0, 1, 2, 3, 4, 6, 7, 8, 9, 10
32+
}
33+
```
34+
35+
## `leave`
36+
37+
If you want to stop a loop, you can use the `leave` keyword anywhere in a `walk` or `while` loop.
38+
39+
```
40+
# walk
41+
walk i = 0 through 10 {
42+
if i == 5 {
43+
leave;
44+
}
45+
46+
bark(i); # outputs 0, 1, 2, 3, 4 (stopping at 5)
47+
}
48+
49+
# while
50+
obj x = 0;
51+
52+
while x != 10 {
53+
if x == 4 {
54+
leave;
55+
}
56+
57+
x = x + 1; # x is incremented as 0, 1, 2, 3, 4
58+
}
59+
```

0 commit comments

Comments
 (0)