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

Zig Flow Control

Zig Flow Control

Zig programming language flow control statements are set by defining one or more conditional statements in the program.

The specified program code is executed when the condition is true, and other specified code is executed when the condition is false.

The following is a typical flow control flowchart:

Zig provides the following control structure statements:

if statement

Syntax

A basic if statement contains a condition and an associated code block; if the condition is true, the code block is executed.

if (

The if-else statement adds an else branch on top of the basic if statement; if the condition in if is false, the code in the else branch is executed.

if () {

    // If condition is true, execute the code here

} else {

    // If condition is false, execute the code here

}

**condition:** a boolean expression. If the condition is true, the first code block is executed; otherwise the else code block is executed.

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

   

const

x

:

i32

=

10

;

   

if

(

x

5

)

{

        std.

debug

.

print

(

"x is greater than 5\n"

, .

{

}

)

;

   

}

else

{

        std.

debug

.

print

(

"x is not greater than 5\n"

, .

{

}

)

;

   

}

}

The compiled execution output is:

x is greater than 5

if-else...else statement

Syntax

Supports multiple condition checks, checking each condition in order until one condition is true and executes the corresponding code block.

Example

if

(

condition1

)

{

   

// if condition1 is true, execute the code here

}

else

if

(

condition2

)

{

   

// if condition2 is true, execute the code here

}

else

{

   

// if all conditions are false, execute the code here

}

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

   

const

x

:

i32

=

10

;

   

if

(

x

10

)

{

        std.

debug

.

print

(

"x is greater than 10\n"

, .

{

}

)

;

   

}

else

if

(

x

==

10

)

{

        std.

debug

.

print

(

"x is equal to 10\n"

, .

{

}

)

;

   

}

else

{

        std.

debug

.

print

(

"x is less than 10\n"

, .

{

}

)

;

   

}

}

The compiled execution output is:

x is equal to 10

Nested if-else

if-else statements can be nested, which means you can include another if statement inside an if or else branch.

if (<条件1>) {

    // If condition 1 is true, execute the code here

    if (<条件2>) {

        // If both condition 1 and condition 2 are true, execute the code here

    } else {

        // If condition 1 is true but condition 2 is false, execute the code here

    }

} else {

    // If condition 1 is false, execute the code here

}

Example

const

std

=

@import

(

"std"

)

;

pub fn main

(

)

void

{

   

const

input

=

5

;

// assume this is the user's input

   

const

max_value

=

10

;

   

if

(

input

max_value

)

{

        std.

debug

.

print

(

"Input is greater than {}\n"

, .

{

max_value

}

)

;

   

}

else

if

(

input

==

max_value

)

{

        std.

debug

.

print

(

"Input is equal to {}\n"

, .

{

max_value

}

)

;

   

}

else

{

       

if

(

input

<

5

)

{

            std.

debug

.

print

(

"Input is less than 5\n"

)

;

       

}

else

{

            std.

debug

.

print

(

"Input is between 5 and {}\n"

, .

{

max_value

}

)

;

       

}

   

}

}

The compiled execution output is:

Input is between 5 and 10


type inference in if statements

The Zig compiler can infer the type of a variable in an if statement if the result of the condition expression is a boolean.

const condition = true;

if (condition) {

    // Here the type of condition is bool, the compiler infers it automatically

}