1. What is Control Flow?
Control flow means the order in which statements and instructions are executed in a program.
Rust provides conditions for decision-making and loops for repeating instructions.
There are two main concepts:
- Conditional Statements – Used to make decisions.
- Loops – Used to execute code repeatedly.
2. if Statement
The if statement executes a block of code only when a given condition is true.
Syntax
Example
Output:
Explanation:
ifchecks a condition.age >= 18is the condition.- If the condition is true, the code inside
{ }executes.
3. if-else Statement
The else block executes when the if condition is false.
Syntax
Example
Output:
4. else-if Statement
Use else if when you need to check multiple conditions.
The first condition that evaluates to true will execute.
Example
Output:
Explanation
- First, Rust checks whether marks are greater than or equal to 90.
- If false, it checks whether marks are greater than or equal to 75.
- The second condition is true, so Grade B is printed.
- The remaining conditions are skipped.
5. Nested if Statement
An if statement inside another if statement is called a nested if statement.
Example
Output:
Nested conditions are useful when one condition depends on another condition.
6. if as an Expression
In Rust, if is an expression, which means it can return a value.
Example
Output:
Important:
- Both branches must return compatible data types.
- Do not put a semicolon after the final expression when you want that expression to return a value.
7. match Expression
The match expression compares a value against different patterns.
It executes the first matching pattern.
Syntax
Example
Output:
What is _ in match?
The underscore _ is called a wildcard pattern.
It matches any value not matched by the previous patterns.
Example
Output:
8. loop Statement
The loop keyword creates an infinite loop.
It continues executing until we explicitly stop it using break.
Syntax
Example
Output:
Explanation
- Initially, count is 1.
- The loop prints the current value.
count += 1increases the value.- When count becomes 6, the break condition becomes true.
- The loop stops.
9. while Loop
The while loop executes code as long as its condition remains true.
Unlike loop, it checks the condition before every iteration.
Syntax
Example
Output:
How while Works
- First, the condition is checked.
- If true, the loop body executes.
- The condition is checked again.
- If false, the loop terminates.
10. for Loop
The for loop is commonly used to iterate over a range or collection.
Syntax
Example
Output:
Understanding Rust Ranges
Rust provides two commonly used range formats.
Range | Meaning |
|---|---|
| 1, 2, 3, 4 |
| 1, 2, 3, 4, 5 |
Important:
..excludes the ending value...=includes the ending value.
Example: Multiplication Table
Output:
11. break Statement
The break statement immediately terminates the current loop.
Example
Output:
When number becomes 5, the loop stops.
12. continue Statement
The continue statement skips the remaining code in the current iteration and moves to the next iteration.
Example
Output:
Number 3 is skipped.
break vs continue
break | continue |
|---|---|
Terminates the loop | Skips the current iteration |
Does not execute further iterations | Continues with the next iteration |
Example: | Example: |
13. Returning a Value from loop
Rust allows a loop to return a value using break.
Example
Output:
Explanation:
- The loop continues until counter becomes 5.
break counter * 2stops the loop and returns 10.- The returned value is stored in
result.
14. Loop Labels
When using nested loops, loop labels help us specify which loop should be stopped or continued.
Rust loop labels start with a single quotation mark.
Example
Output:
Here, break 'outer terminates the outer loop directly.
15. Comparison of Rust Loops
Loop | Purpose |
|---|---|
| Repeats indefinitely until stopped |
| Repeats while a condition is true |
| Iterates over a range or collection |
16. Important Summary
Concept | Purpose |
|---|---|
if | Executes code when a condition is true |
else | Executes code when the if condition is false |
else if | Checks multiple conditions |
Nested if | Places one if statement inside another |
match | Matches a value against patterns |
loop | Creates an indefinite loop |
while | Repeats while a condition is true |
for | Iterates over ranges and collections |
break | Terminates a loop |
continue | Skips the current iteration |
Loop labels | Control nested loops |