Skip to content

Latest commit

 

History

History
272 lines (192 loc) · 8.5 KB

03_control_flow.md

File metadata and controls

272 lines (192 loc) · 8.5 KB

🦀 30 Days Of Rust: Day 3 - Control Flow 🚀

LinkedIn Follow me on GitHub

Author: Het Patel

October, 2024

<< Day 2 | Day 4 >>

30DaysOfRust


📘 Day 3 - Control Flow

👋 Welcome

Welcome to Day 3 of the 30 Days of Rust! 🎉 Today, we will explore Control Flow in Rust. Understanding control flow is essential for directing the flow of your program based on conditions, repeating actions, or managing various scenarios effectively. Let’s dive into it! 🚀

Congratulations! 🎉 You've taken the first step in your journey to master the 30 Days of Rust programming challenge. In this challenge, you will learn the fundamentals of Rust and how to harness its power to write efficient, fast, and safe code. By the end of this journey, you'll have gained a solid understanding of Rust's core concepts and best practices, helping you become a confident Rustacean. 🦀

Feel free to join the 30 Days of Rust community on Discord, where you can interact with others, ask questions, and share your progress!

🔍 Overview

In Rust, control flow allows you to:

  • Make decisions using conditional statements.
  • Repeat actions using different types of loops.
  • Handle multiple scenarios efficiently.

We will cover:

  • if, else, and else if statements
  • Various loop mechanisms: loop, while, and for
  • The powerful match statement for pattern matching

🛠 Environment Setup

Ensure that you have your Rust environment set up correctly from Day 1. If you haven’t installed Rust yet, please refer to the setup instructions from Day 1.

📖 Control Flow in Rust

🧩 The if Statement

The if statement lets you execute a block of code only if a specified condition is true.

Example:

fn main() {
    let number = 10;

    if number > 5 {
        println!("The number is greater than 5");
    }
}

Output:

The number is greater than 5

🔄 The else and else if Statements

Use else and else if to add alternative conditions.

Example:

fn main() {
    let age = 18;

    if age >= 21 {
        println!("You can drink alcohol in the INDIA.");
    } else if age >= 18 {
        println!("You are an adult, but cannot drink alcohol in the INDIA.");
    } else {
        println!("You are a minor.");
    }
}

Output:

You are an adult, but cannot drink alcohol in the INDIA.

🔁 The loop Statement

The loop statement repeatedly executes a block of code. To break out, use the break keyword.

Example:

fn main() {
    let mut count = 0;

    loop {
        count += 1;
        if count == 3 {
            println!("Breaking the loop at count: {}", count);
            break;
        }
    }
}

Output:

Breaking the loop at count: 3

🔄 The while Loop

The while loop continues to run while a condition is true.

Example:

fn main() {
    let mut num = 1;

    while num <= 5 {
        println!("Number is: {}", num);
        num += 1;
    }
}

Output:

Number is: 1
Number is: 2
Number is: 3
Number is: 4
Number is: 5

🔁 The for Loop

The for loop is useful for iterating over a collection or a range of numbers.

Example:

fn main() {
    for num in 1..4 {
        println!("Num: {}", num);
    }
}

Output:

Num: 1
Num: 2
Num: 3

🛑 Control Flow with match

The match statement lets you handle multiple scenarios by pattern matching.

Example:

fn main() {
    let traffic_light = "green";

    match traffic_light {
        "green" => println!("Go"),
        "yellow" => println!("Slow down"),
        "red" => println!("Stop"),
        _ => println!("Invalid color"),
    }
}

Output:

Go

🎯 Hands-On Challenge

Write a program that:

  1. Asks the user to input a number.
  2. Uses an if statement to check if the number is even or odd.
  3. Use a loop to print numbers from 1 to 5.
  4. Implement a match statement to respond to different days of the week, e.g., "Monday" => "Start of the week!", "Friday" => "Weekend is coming!", etc.

💻 Exercises - Day 3

✅ Exercise: Level 1

  1. Write a program that checks if a number is even or odd using the if statement.
  2. Create a while loop that prints numbers from 1 to 10.
  3. Use the for loop to iterate over an array of your favorite colors and print each one.
  4. Create a simple calculator using the match statement that performs addition, subtraction, multiplication, or division based on user input.
  5. Write a program that continuously takes user input until the word "exit" is typed, using a loop.

✅ Exercise: Level 2

  1. Create a program that calculates the factorial of a given number using a while loop.
  2. Write a program that simulates a countdown timer using a loop and breaks when the countdown reaches zero.
  3. Use the for loop to calculate the sum of even numbers from 1 to 50.
  4. Write a program that reads a string input and uses the match statement to respond with different outputs based on the input (e.g., "hello" => "Hi there!", "bye" => "Goodbye!", etc.).
  5. Implement a program that uses if statements inside a for loop to print all the odd numbers from 1 to 20.
  6. Create a small game where the program generates a random number between 1 and 10, and the user has to guess it. Use a loop to keep asking until the user gets it right.

🎥 Helpful Video References

📝 Day 3 Summary

  • Learned about using if, else, and else if for conditional checks.
  • Explored loop, while, and for loops to repeat actions.
  • Discovered the match statement for effective control flow management.

🌟 Great job on completing Day 3! Keep practicing, and get ready for Day 3 where we will explore Functions in Rust!

Thank you for joining Day 3 of the 30 Days of Rust challenge! If you found this helpful, don’t forget to Star GIF star this repository, share it with your friends, and stay tuned for more exciting lessons ahead!

Stay Connected
📧 Email: Hunterdii
🐦 Twitter: @HetPate94938685
🌐 Website: Working On It(Temporary)

<< Day 2 | Day 4 >>