-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.rs
68 lines (53 loc) · 1.67 KB
/
loop.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* Loop
* A loop statement allows us to execute a statement or group of statements multiple times.
* Rust provides different types of loops to handle looping requirements.
*
* ~ List of supported loop in rust
* - while
* - loop
* - for
*/
fn main() {
// Whle Loop.
// Rust Loop.
println!("-------------------- For Loop --------------------");
// For Loop.
// The for loop executes the code block for a specified number of times.
// It can be used to iterate over a fixed set of values, such as an array.
for i in 1..10 {
println!("{}", i);
}
println!("-------------------- For Loop With Continue --------------------");
// For loop with continue statement.
// The continue statement skips the subsequent statements in the current iteration and takes the control back to the beginning of the loop.
for x in 1..11 {
if x == 5 {
continue;
}
println!("{}", x);
}
println!("-------------------- While Loop --------------------");
let mut num = 0;
while num < 10 {
println!("{}", num);
num = num + 1;
}
println!("-------------------- While Loop --------------------");
let mut num_2 = 1;
while num_2 <= 20 {
println!("{}", num_2);
num_2 += 1;
}
println!("-------------------- Rust Loop --------------------");
// Rust Loop.
// The break statement is used to take the control out of a construct. Using break in a loop causes the program to exit the loop.
let mut count = 0;
loop {
println!("{}", count);
count += 1;
if count == 15 {
break;
}
}
}