2023-10-02 01:41:38 +00:00
|
|
|
# Loops
|
|
|
|
|
|
|
|
## For loop
|
|
|
|
|
|
|
|
Braces are required.
|
|
|
|
|
|
|
|
```thp
|
2024-02-20 10:17:21 +00:00
|
|
|
val numbers = [0, 1, 2, 3]
|
2023-10-02 01:41:38 +00:00
|
|
|
|
|
|
|
for number in numbers
|
|
|
|
{
|
|
|
|
print(number)
|
|
|
|
}
|
|
|
|
|
|
|
|
for #(index, number) in numbers.entries()
|
|
|
|
{
|
|
|
|
print("{index} => {number}")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```thp
|
2024-02-20 10:17:21 +00:00
|
|
|
val dict = Obj {
|
2024-01-01 14:11:41 +00:00
|
|
|
apple: 10,
|
|
|
|
banana: 7,
|
|
|
|
cherries: 3,
|
2023-10-02 01:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for #(key, value) in dict
|
|
|
|
{
|
|
|
|
print("{key} => {value}")
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```thp
|
|
|
|
for value in collection
|
|
|
|
{
|
|
|
|
if condition
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## While loop
|
|
|
|
|
|
|
|
```thp
|
2024-02-20 10:17:21 +00:00
|
|
|
val colors = ["red", "green", "blue"]
|
|
|
|
val mut index = 0
|
2023-10-02 01:41:38 +00:00
|
|
|
|
|
|
|
while index < colors.size()
|
|
|
|
{
|
2024-01-01 14:11:41 +00:00
|
|
|
print("{colors[index]}")
|
2023-10-02 01:41:38 +00:00
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## Infinite loop
|
|
|
|
|
2023-10-24 01:39:34 +00:00
|
|
|
Basically Rust*'s loop.
|
2023-10-02 01:41:38 +00:00
|
|
|
|
|
|
|
```thp
|
|
|
|
loop
|
|
|
|
{
|
|
|
|
print("looping")
|
|
|
|
|
|
|
|
if condition
|
|
|
|
{
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-10-24 01:39:34 +00:00
|
|
|
* Rust is a trademark of the Rust Foundation. THP is not affiliated,
|
|
|
|
endorsed or supported by the Rust Foundation.
|
2023-10-02 01:41:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|