Zig Loop
In Zig, loop structures include `while` loops and `for` loops. Each type of loop has its specific syntax and usage. Below is a detailed introduction to these two loop structures, including their syntax, description, and examples.
## 1. `while` Loop
### Syntax
while (condition) : (increment) { // code block}
* `condition`: A boolean expression, as long as it is `true`, the loop will continue to execute.
* `increment` (optional): An expression that executes after each loop iteration.
### Description
The `while` loop checks the condition before each iteration. If the condition is `true`, the code in the loop body is executed. After the loop body is executed, the `increment` expression is executed (if present). Then the condition is checked again, until the condition becomes `false`.
### Example
## Example
const std = @import("std");
pub fn main()void{
var i: i32 =0;
while(i <5):(i +=1){
std.debug.print("i: {}n", .{i});
}
}
Analysis:
* Initialize variable `i` to 0.
* As long as `i` is less than 5, the loop body will execute.
* After each iteration, `i` is incremented by 1.
Code compilation and execution result 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
The `for` loop is used to iterate through 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 =i32{1, 2, 3, 4, 5};
var index: usize =0;// Index variable needs type declaration
for(array)|item|{
std.debug.print("index: {}, item: {}n", .{ index, item });
index +=1;// Update index
}
}
Analysis:
* Define an array containing 5 integers.
* Use `for` loop to iterate through each element in the array, assigning the element value to `item` and the index to `index`.
* In the loop body, print the value and index of each element.
Code compilation and execution result 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 when i equals 5
}
if(i ==8){
break;// Terminate the loop
}
std.debug.print("i: {}n", .{i});
}
}
Analysis:
* When `i` equals 5, `continue` skips that iteration.
* When `i` equals 8, `break` terminates the loop.
Code compilation and execution result 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
Containing another loop within a loop body is called nested loops. 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 through the array
for(letters)|letter|{
var count: i32 =0;// Declare counter
// Use while loop to print each letter and count
while(count <3):(count +=1){
std.debug.print("{s} - {}n", .{ letter, count });
}
}
}
Analysis:
* The outer `for` loop iterates through the letters array.
* The inner `while` loop executes 3 times, printing the letter and count.
Code compilation and execution result 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 contain some exit mechanism, otherwise the program will run forever
}
}
### Range Loop
Zig also allows you to use ranges for looping, which can simplify code in certain situations:
## Example
pub fn main()void{
for(0..10)|i|{
std.debug.print("i: {}n", .{i});
}
}
### Labeled Loop
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.print("Inside nested loopn");
break:loop;// Exit the outer loop
}
}
}
YouTip