codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

8 / 58 topics
8Control Flow9If-Else Statements10Loops
Tutorials/Rust/Control Flow
🦀Rust

Control Flow

Updated 2026-05-15
10 min read

Control Flow

Introduction

In programming, control flow refers to the order in which instructions are executed. It allows developers to make decisions based on conditions and repeat actions multiple times. Rust provides several constructs for controlling the flow of a program, including conditional statements and loops.

Concept

Conditional Statements

Conditional statements allow your program to execute different blocks of code based on certain conditions. The most common conditional statement in Rust is the if expression.

If-Else Statement

The basic structure of an if statement is as follows:

let number = 3;

if number < 5 {
    println!("condition was true");
} else {
    println!("condition was false");
}

In this example, the program checks if `number` is less than 5. If the condition is true, it prints "condition was true". Otherwise, it prints "condition was false".

#### Multiple Conditions

You can use multiple conditions by combining them with logical operators like `&&` (and) and `||` (or):

```rust
let number = 6;

if number % 4 == 0 {
    println!("number is divisible by 4");
} else if number % 3 == 0 {
    println!("number is divisible by 3");
} else if number % 2 == 0 {
    println!("number is divisible by 2");
} else {
    println!("number is not divisible by 4, 3, or 2");
}

In this example, the program checks multiple conditions and executes the block of code associated with the first true condition.

### Loops

Loops allow you to repeat a block of code until a certain condition is met. Rust provides three types of loops: `loop`, `while`, and `for`.

#### Loop

The `loop` keyword creates an infinite loop that continues indefinitely until it is explicitly broken using the `break` statement:

```rust
let mut counter = 0;

loop {
    println!("Counter: {}", counter);
    counter += 1;
    if counter == 5 {
        break;
    }
}

In this example, the program prints the value of `counter` and increments it by 1 in each iteration. The loop breaks when `counter` reaches 5.

#### While Loop

The `while` loop continues to execute as long as a condition is true:

```rust
let mut number = 3;

while number != 0 {
    println!("{}!", number);
    number -= 1;
}

println!("LIFTOFF!!!");

In this example, the program prints the value of number and decrements it by 1 in each iteration. The loop stops when number becomes 0.

For Loop

The for loop is used to iterate over a collection, such as an array or range:

let a = [10, 20, 30, 40, 50];

for element in a.iter() {
    println!("the value is: {}", element);
}

In this example, the program iterates over each element in the array a and prints its value.

Examples

Conditional Statements Example

fn main() {
    let number = 6;

    if number % 4 == 0 {
        println!("number is divisible by 4");
    } else if number % 3 == 0 {
        println!("number is divisible by 3");
    } else if number % 2 == 0 {
        println!("number is divisible by 2");
    } else {
        println!("number is not divisible by 4, 3, or 2");
    }
}

Loops Example

fn main() {
    let mut counter = 0;

    loop {
        println!("Counter: {}", counter);
        counter += 1;
        if counter == 5 {
            break;
        }
    }

    let mut number = 3;

    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }

    println!("LIFTOFF!!!");

    let a = [10, 20, 30, 40, 50];

    for element in a.iter() {
        println!("the value is: {}", element);
    }
}

What's Next?

In the next section, we will explore more advanced control structures, including if-else statements and loops with labels. Understanding these concepts will help you write more complex and efficient Rust programs.


PreviousOperatorsNext If-Else Statements

Recommended Gear

OperatorsIf-Else Statements