-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloops.qs
More file actions
63 lines (53 loc) · 1.22 KB
/
Copy pathloops.qs
File metadata and controls
63 lines (53 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Loops
# repeat N runs the block N times.
counter = 0
repeat 3 {
counter += 1
print("counter:", counter)
}
# repeat item -> number counts from 1 to that number.
repeat index -> 5 {
print("index:", index)
}
# repeat item -> string gives one letter each time.
repeat letter -> "abc" {
print("letter:", letter)
}
# repeat item -> list gives one item each time.
repeat color -> ["red", "green", "blue"] {
print("color:", color)
}
# repeat item -> map gives one key each time.
person = map("name"="Sara", "age"=20)
repeat key -> person {
print("key:", key, "value:", person[key])
}
# repeat i, item -> collection gives index and value.
names = ["Ali", "Sara", "Mina"]
repeat i, name -> names {
print(i, name)
}
# range creates a list of numbers for looping.
print(range(5)) # [1, 2, 3, 4, 5]
print(range(2, 5)) # [2, 3, 4, 5]
print(range(5, 1, -2)) # [5, 3, 1]
# break exits the loop early.
repeat n -> range(1, 10) {
if n == 4 {
break
}
print("n:", n)
}
# continue skips to the next iteration.
repeat n -> range(1, 6) {
if n == 3 {
continue
}
print("skip:", n)
}
# while runs while a condition is true.
x = 0
while x < 3 {
x += 1
print("while x:", x)
}