Skip to content

Commit a32c4f1

Browse files
committed
control flows addition
1 parent db5c620 commit a32c4f1

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

05. control_flow.md

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
### 5. Control Flow: Conditional Statements and Loops
2+
3+
#### Conditional Statements: `if`, `else if`, `else`
4+
5+
Conditional statements in Rust allow you to execute different blocks of code based on certain conditions. The basic syntax for `if` statements is as follows:
6+
7+
```rust
8+
let number = 10;
9+
10+
if number > 0 {
11+
println!("Number is positive");
12+
} else if number < 0 {
13+
println!("Number is negative");
14+
} else {
15+
println!("Number is zero");
16+
}
17+
```
18+
19+
- The `if` statement checks the condition and executes the block of code if the condition is true.
20+
- The `else if` statement allows you to check additional conditions if the previous condition is false.
21+
- The `else` statement is optional and executes if none of the previous conditions are true.
22+
23+
#### Pattern Matching with `match`
24+
25+
Pattern matching in Rust provides a concise way to compare values against a series of patterns and execute code based on the matched pattern. Here's an example:
26+
27+
```rust
28+
let number = 42;
29+
30+
match number {
31+
0 => println!("Zero"),
32+
1 => println!("One"),
33+
_ => println!("Other"),
34+
}
35+
```
36+
37+
- The `match` keyword is followed by the value to match against.
38+
- Each arm of the match consists of a pattern followed by the `=>` operator and the code to execute if the pattern matches.
39+
- The `_` wildcard pattern matches any value and is often used as a catch-all case.
40+
41+
#### Looping Constructs: `loop`, `while`, `for`
42+
43+
Rust provides several looping constructs for executing code repeatedly.
44+
45+
##### `loop`
46+
47+
The `loop` keyword creates an infinite loop that continues until explicitly interrupted with a `break` statement.
48+
49+
```rust
50+
let mut counter = 0;
51+
52+
loop {
53+
println!("Counter: {}", counter);
54+
counter += 1;
55+
56+
if counter >= 10 {
57+
break;
58+
}
59+
}
60+
```
61+
62+
##### `while`
63+
64+
The `while` loop executes a block of code as long as the specified condition is true.
65+
66+
```rust
67+
let mut counter = 0;
68+
69+
while counter < 10 {
70+
println!("Counter: {}", counter);
71+
counter += 1;
72+
}
73+
```
74+
75+
##### `for`
76+
77+
The `for` loop iterates over elements of an iterator.
78+
79+
```rust
80+
let numbers = [1, 2, 3, 4, 5];
81+
82+
for number in numbers.iter() {
83+
println!("Number: {}", number);
84+
}
85+
```
86+
87+
#### Early Exit with `break` and `continue`
88+
89+
- The `break` statement exits the current loop immediately.
90+
- The `continue` statement skips the rest of the current iteration and proceeds to the next iteration of the loop.
91+
92+
#### Iterating Over Collections with `for` Loop and Iterators
93+
94+
- Rust's `for` loop is particularly useful for iterating over collections like arrays, vectors, and ranges.
95+
- You can also use iterators, which provide a powerful way to work with sequences of elements in Rust.
96+
97+
#### Best Practices
98+
99+
- Use `if`, `else if`, and `else` for simple conditional branching, and `match` for more complex pattern matching scenarios.
100+
- Prefer `for` loops and iterators for iterating over collections, as they are more concise and idiomatic in Rust.
101+
- Use `loop` when you need to create an infinite loop with explicit exit conditions.
102+
103+
#### Real-World Example
104+
105+
Imagine you're developing a text adventure game in Rust. You use conditional statements to handle different player choices and looping constructs to manage game flow and interactions.
106+
107+
#### Conclusion
108+
109+
Understanding control flow constructs in Rust is essential for writing expressive and efficient code. By mastering these concepts, you'll have the tools to build complex algorithms and applications with confidence.

0 commit comments

Comments
 (0)