ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.