Rust program to demonstrate the continue statement with while and for loops

Rust | continue statement with while and for loops: Write an example to demonstrate the example of continue statement with while and for loops.
Submitted by Nidhi, on October 05, 2021

Problem Solution:

In this program, we will use the continue statement with nested while loop. The continue statement is used to skip the below statements in the loop body.

Program/Source Code:

The source code to demonstrate the continue statement with while and for loop is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// continue statement with while and for loops

fn main() {
    let mut cnt:i32 = 0;
    
    while cnt<=9
    {
        cnt=cnt+1;
        if cnt==5
        {
         continue;
        }
        print!("{} ",cnt);
    }
    
    println!();
    
    for cnt in 1..11
    {
        if cnt==5
        {
            continue;
        }
        print!("{} ",cnt);
    }    
}

Output:

1 2 3 4 6 7 8 9 10 
1 2 3 4 6 7 8 9 10 

Explanation:

Here, we used the continue statement with while and for loop to skip the below statements in the loop body when the value of the cnt variable is equal to 5.

Rust Looping Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.