Zig If
The flow control statements in the Zig programming language are set up by defining one or more conditional statements in a program.
When the condition is true, the specified program code is executed; when the condition is false, other specified code is executed.
Here is a typical flow control flowchart:

Zig provides the following control structure statements:
## if Statement
### Syntax
A basic if statement includes a condition and a code block associated with it. If the condition is true, the code block is executed.
if () { // If the condition is true, execute the code here}
The if-else statement adds an else branch on top of the basic if statement. If the condition in the if statement is false, the code in the else branch is executed.
if () { // If the condition is true, execute the code here} else { // If the 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 5n", .{});
} else {
std.debug.print("x is not greater than 5n", .{});
}
}
Compilation and execution output:
x is greater than 5
## if-else...else Statement
### Syntax
Multiple conditions can be checked, and each condition is checked sequentially until a condition becomes true and its corresponding code block is executed.
## 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 10n", .{});
} else if (x == 10) {
std.debug.print("x is equal to 10n", .{});
} else {
std.debug.print("x is less than 10n", .{});
}
}
Compilation and execution output:
x is equal to 10
## Nested if-else
The if-else statement can be nested, meaning you can include another if statement within the if or else branch.
if () { // If condition1 is true, execute the code here if () { // If both condition1 and condition2 are true, execute the code here } else { // If condition1 is true but condition2 is false, execute the code here } } else { // If condition1 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 5n");
} else {
std.debug.print("Input is between 5 and {}n", .{max_value});
}
}
}
Compilation and execution output:
Input is between 5 and 10
* * *
## Type Inference in if Statements
The Zig compiler can infer the type of variables in if statements if the result of the conditional expression is a boolean value.
const condition = true;
if (condition) { // Here, the type of condition is bool, automatically inferred by the compiler}
YouTip