Skip to content

Commit d67d34a

Browse files
committed
Feat: add control flow section
1 parent 4dbba8d commit d67d34a

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

CONTROL_FLOW/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CONTROL_FLOW/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "control_flow"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

CONTROL_FLOW/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Read more details here:
2+
3+
https://doc.rust-lang.org/book/ch03-05-control-flow.html#conditional-loops-with-while

CONTROL_FLOW/src/main.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// ====== if Expressions ======
2+
3+
// fn main() {
4+
// let number = 3;
5+
6+
// if number % 4 == 0 {
7+
// println!("number is divisible by 4");
8+
// } else if number % 3 == 0 {
9+
// println!("number is divisible by 3");
10+
// } else if number % 2 == 0 {
11+
// println!("number is divisible by 2");
12+
// } else {
13+
// println!("number is not divisible by 4, 3, or 2");
14+
// }
15+
// }
16+
17+
// fn main() {
18+
// let condition = true;
19+
// let number = if condition { 5 } else { 6 };
20+
21+
// println!("The value of number is: {number}");
22+
// }
23+
24+
// ====== Repetition with Loops ======
25+
// 3 kinds of loops:
26+
// loop,
27+
// while, and
28+
// for.
29+
// fn main() {
30+
// loop {
31+
// println!("again!");
32+
// // break;
33+
// }
34+
// }
35+
36+
// fn main() {
37+
// let mut counter = 0;
38+
// let result = loop {
39+
// counter += 1;
40+
// if counter == 10 {
41+
// break counter * 2;
42+
// }
43+
// };
44+
45+
// println!("The result is {result}")
46+
// }
47+
48+
// ====== Loop Labels to Disambiguate Between Multiple Loops ======
49+
// fn main() {
50+
// let mut count = 0;
51+
// 'counting_up: loop {
52+
// println!("count = {count}");
53+
// let mut remaining = true;
54+
55+
// loop {
56+
// println!("remaining = {remaining}");
57+
// if remaining == false {
58+
// break;
59+
// }
60+
// if count == 2 {
61+
// break 'counting_up;
62+
// }
63+
// remaining = false;
64+
// }
65+
66+
// count += 1;
67+
// }
68+
// println!("End count = {count}");
69+
// }
70+
71+
// ====== Conditional Loops with while ======
72+
// fn main() {
73+
// let mut number = 3;
74+
// while number != 0 {
75+
// println!("{number}!");
76+
// number -= 1;
77+
// }
78+
79+
// println!("LEFTOFF!!!");
80+
// }
81+
82+
// fn main() {
83+
// let a = [10, 20, 30, 40, 50];
84+
// let mut index = 0;
85+
86+
// while index < 5 {
87+
// println!("the value is: {}", a[index]);
88+
89+
// index += 1;
90+
// }
91+
// }
92+
93+
// fn main() {
94+
// let a = [10, 20, 30, 40, 50];
95+
96+
// for element in a {
97+
// println!("the value is: {element}");
98+
// }
99+
// }
100+
101+
fn main() {
102+
// reverse the range
103+
for number in (1..4).rev() {
104+
println!("{number}!");
105+
}
106+
println!("LIFTOFF!!!");
107+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
- [GUESSING_GAME](./GUESSING_GAME/)
66
- [VARIABLES_AND_DATA_TYPES](./VARIABLES_AND_DATA_TYPES/)
77
- [FUNCTIONS](./FUNCTIONS/)
8+
- [CONTROL_FLOW](./CONTROL_FLOW/)

0 commit comments

Comments
 (0)