Zig Loops
In Zig, loop structures include while loops and for loops. Each loop has its specific syntax and usage. The following details these two loop structures, including their syntax, descriptions, and examples.
1. while Loop
Syntax
while (condition) : (increment) {
// code block
}
condition: a boolean expression, as long as it istrue, the loop will continue to execute.increment(optional): an expression that executes after each loop iteration.
Description
while The loop checks the condition before each iteration. If the condition is true, then the code in the loop body is executed. After the loop body finishes executing, the increment expression (if any) is executed. Then the condition is checked again until the condition is false.
Example
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
var i
:
i32
=
0
;
while
(
i
<
5
):
(
i
=
1
)
{
std.
debug
.
(
"i: {}\n"
, .
{
i
}
)
;
}
}
Analysis:
- Initialize variables
iis 0. - As long as
iis less than 5, the loop body will execute. - After each loop iteration,
iincrements by 1.
The compilation and execution result of the code is:
i: 0
i: 1
i: 2
i: 3
i: 4
2. for Loop
Syntax
for (collection) |item, index| {
// code block
}
collection: an array, slice, or other iterable collection.item: the current element of the collection in each iteration.index(optional): the index of the current element.
Description
for The loop is used to iterate over each element in a collection. In each iteration, the current element of the collection is assigned to item, and the code in the loop body is executed.
Example
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
const
array
=
[
5
]
i32
{
1
,
2
,
3
,
4
,
5
}
;
var index
:
usize
=
0
;
// The index variable needs to be declared with a type
for
(
array
)
|
item
|
{
std.
debug
.
(
"index: {}, item: {}\n"
, .
{
index, item
}
)
;
index
=
1
;
// Update the index
}
}
Analysis:
- defines an array containing 5 integers.
- Use
forThe loop iterates over each element in the array and assigns the element value toitem, and the index toindex. - In the loop body, print the value and index of each element.
The compilation and execution result of the code is:
index: 0, item: 1
index: 1, item: 2
index: 2, item: 3
index: 3, item: 4
index: 4, item: 5
3. continue and break
Syntax
continue: skip the current iteration and continue to the next iteration.break: terminate the loop.
Description
continue is used to skip the remaining code in the current iteration and immediately start the next iteration.break is used to terminate the entire loop.
Example
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
var i
:
i32
=
0
;
while
(
i
<
10
):
(
i
=
1
)
{
if
(
i
==
5
)
{
continue
;
// Skip the iteration where i equals 5
}
if
(
i
==
8
)
{
break
;
// terminate the loop
}
std.
debug
.
(
"i: {}\n"
, .
{
i
}
)
;
}
}
Analysis:
- When
iWhen equals 5,continueskip that iteration. - When
iWhen equals 8,breakterminate the loop.
The compilation and execution result of the code is:
i: 0
i: 1
i: 2
i: 3
i: 4
i: 6
i: 7
5. Nested Loops
Syntax
for (outer_collection) |outer_item| {
for (inner_collection) |inner_item| {
// code block
}
}
Description
A loop that contains another loop within its body is called a nested loop. In each iteration of the outer loop, the inner loop is executed.
Example
Example
const
std
=
@import
(
"std"
)
;
pub fn main
(
)
void
{
// Declare an array containing three strings
const
letters
=
[
_
]
[
]
const
u8
{
"A"
,
"B"
,
"C"
}
;
// Iterate over the array
for
(
letters
)
|
letter
|
{
var count
:
i32
=
0
;
// Declare a counter
// Use a while loop to print each letter and count
while
(
count
<
3
):
(
count
=
1
)
{
std.
debug
.
(
"{s} - {}\n"
, .
{
letter, count
}
)
;
}
}
}
Analysis:
- Outer
forThe loop iterates over the array of letters. - Inner
whileThe loop executes 3 times, printing the letters and counts.
The compilation and execution result of the code is:
A - 0
A - 1
A - 2
B - 0
B - 1
B - 2
C - 0
C - 1
C - 2
6. More Loops
Infinite loop
You can use a while loop to create an infinite loop, as long as the condition is always true:
Example
pub fn main
(
)
void
{
while
(
true
)
{
// Code block
// Must include some exit mechanism, otherwise the program will run forever
}
}
Range Loops
Zig also allows you to loop using a range, which can simplify code in some situations:
Example
pub fn main
(
)
void
{
for
(
0
..
10
)
|
i
|
{
std.
debug
.
(
"i: {}\n"
, .
{
i
}
)
;
}
}
Labeled Loops
Zig supports labeled loops, allowing you to name loops, which is very useful in nested loops:
Example
pub fn main
(
)
void
{
loop
:
while
(
true
)
{
while
(
true
)
{
std.
debug
.
(
"Inside nested loop\n"
)
;
break
:
loop
;
// Exit the outer loop
}
}
}