Instagram
youtube
Facebook
Twitter

Loop

Loop

Loop statements are used to repeat a piece of code for a given number of times or until it satisfies a condition.

Syntax:

loop {
    // Statements(s)
}

Example

fn main() {
    let mut n = 1;

    loop {
        n += 1;

        println!("Value: {}", n);

        if n > 55 {
            break;
        }

        
    }

    println!("Value of n is {}", n);

    let l = 12..33;

    for i in l {
        println!("THE VALUE IS {}", i);
    }

Break keyword is used here to terminate the loop from further iterating, and if there is not break keyword then loop will execute for indefinitely amount of time i.e. it won'r stop.

Output

prints value till 56 and then loop breaks and then other loop starts looping from 12 to 32