-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloops.rs
50 lines (45 loc) · 1.14 KB
/
loops.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
/**
* In this module we will go through few examples and explain loops in rust lang.
* For Loop. (line 12)
* While Loop. (line 22)
* Loop keyword use example. (line 30)
* Continue statement. (line 40)
*/
pub mod module {
pub fn loops() {
// Definite Loop
// Using For Loop example.
for x in 1..11 { // 11 is not inclusive
if x == 5 {
continue;
}
println!("x is {}",x);
}
// Indefinite Loops.
// While loop example.
let mut y = 0;
while y < 10 {
y += 1;
println!("inside loop x value is {}",y);
}
println!("outside loop x value is {}",y);
// Loop example.
let mut z = 0;
loop {
z += 1;
println!("z={}",z);
if z == 15 {
break;
}
}
// Using continue statement.
let mut count = 0;
for num in 0..21 {
if num % 2==0 {
continue;
}
count+=1;
}
println! (" The count of odd values between 0 and 20 is: {} ",count);
}
}