Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Zig Loops

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

}

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

.

print

(

"i: {}\n"

, .

{

i

}

)

;

   

}

}

Analysis:

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

}

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

.

print

(

"index: {}, item: {}\n"

, .

{

index, item

}

)

;

        index

=

1

;

// Update the index

   

}

}

Analysis:

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

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

.

print

(

"i: {}\n"

, .

{

i

}

)

;

   

}

}

Analysis:

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

.

print

(

"{s} - {}\n"

, .

{

letter, count

}

)

;

       

}

   

}

}

Analysis:

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

.

print

(

"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

.

print

(

"Inside nested loop\n"

)

;

           

break

:

loop

;

// Exit the outer loop

       

}

   

}

}