Difference between revisions of "Rust-Programming-Language/C2/Control-Flow/English"
(Created page with "{| border="1" |- || '''Visual Cue''' || '''Narration''' |- || '''Slide 1''' || <span style="color:#000000;">Welcome to the Spoken Tutorial on </span><span style="color:#00000...") |
|||
| Line 6: | Line 6: | ||
|| '''Slide 1''' | || '''Slide 1''' | ||
| − | || | + | || Welcome to the Spoken Tutorial on '''Control Flow '''in '''Rust'''. |
| − | |- | + | |- |
| − | || | + | || '''Slide 2''' |
| − | + | '''Learning Objectives''' | |
|| In this tutorial, we will learn about | || In this tutorial, we will learn about | ||
| − | * Control Flow Statement | + | * Control Flow Statement |
| − | |- | + | |- |
| − | || | + | || '''Slide 3''' |
| − | + | '''System Requirements''' | |
| − | || | + | || This tutorial is recorded using |
| − | * | + | * Ubuntu Linux OS version 22.04 |
| − | * | + | * Rust 1.80.0 |
| − | * | + | * Visual Studio Code 1.19.0 |
| − | |- | + | |- |
| − | || | + | || '''Slide 4''' |
| − | + | '''Prerequisites''' | |
| − | || | + | || To follow this tutorial, |
| − | * | + | * You should be familiar with compiling and running a Rust program. |
| − | |- | + | |- |
| − | || | + | || '''Slide''' '''5''' |
| − | + | '''Code Files''' | |
|| | || | ||
| − | * | + | * The following code file is required to practise this tutorial |
| − | * | + | * This file is provided in the Code Files link of this tutorial page |
| − | |- | + | |- |
| − | | | + | || |
| − | || | + | || We will see about conditional statements in Rust. |
| − | |- | + | |- |
| − | || | + | || '''Slide 6''' |
| − | + | '''Conditional Statements''' | |
| − | || | + | || Rust supports |
| − | * | + | * if |
| − | * | + | * else if |
| − | * | + | * else |
| − | statements | + | statements for making decisions based on conditions. |
| − | It | + | It always returns a '''boolean''' value. |
| − | |- | + | |- |
| − | || | + | || Open '''Visual''' '''code editor''' |
| − | || | + | || Let us open the '''visual code editor''' and understand the control statements with examples. |
| − | |- | + | |- |
| − | | | + | || |
| − | || | + | || In the menu bar, click on '''Terminal''' and select''' New Terminal''' |
| − | + | We can see a terminal window at the bottom. | |
| − | |- | + | |- |
|| > cd '''MyRustProject''' | || > cd '''MyRustProject''' | ||
| Line 71: | Line 71: | ||
Open the created project as shown. | Open the created project as shown. | ||
| − | |- | + | |- |
| − | || | + | || |
| + | || In the '''main.rs '''file, copy and paste the code from the code file. | ||
| + | |- | ||
| + | || fn main() { | ||
| − | + | let n = 100; | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | if n>100{ | |
| − | + | println!("Greater than 100"); | |
| − | + | }else if n==100{ | |
| − | + | println!("Equal to 100"); | |
| − | + | }else { | |
| − | + | println!("Smaller than 100"); | |
| − | + | } | |
| − | + | } | |
| + | || Let us look at this example for an''' if else''' statement. | ||
| − | + | If n is greater than 100, it prints a “greater than 100” statement. | |
| − | + | ||
| − | + | If n equals 100, the second print statement will be executed. | |
| − | + | If n is less than 100, it prints “smaller than 100”. | |
| − | + | You can include multiple '''else if''' statements to check multiple conditions in sequence. | |
| − | + | The condition must be true to run the code wrapped to it. | |
| − | + | Press '''Ctrl and S '''to save the file. | |
| − | + | ||
| − | + | ||
Now let us run the program. | Now let us run the program. | ||
| − | |- | + | |- |
| + | || In the menu bar, click on '''Terminal''' and select '''New Terminal.''' | ||
|| In the menu bar, click on '''Terminal''' and select '''New Terminal.''' | || In the menu bar, click on '''Terminal''' and select '''New Terminal.''' | ||
| − | |||
| − | |- | + | |- |
|| > cargo run | || > cargo run | ||
| − | || | + | || In the terminal, type '''cargo run '''to see the output. |
| − | The output shows '''“equal to 100” '''as we assigned '''n '''as 100 in the program. | + | The output shows '''“equal to 100” '''as we assigned '''n ''' as 100 in the program. |
| − | |- | + | |- |
| − | | | + | || |
| − | || | + | || Next let us see '''loop''' statements in Rust. |
| − | |- | + | |- |
|| Slide | || Slide | ||
Loop | Loop | ||
|| | || | ||
| − | * | + | * A loop is used to repeat a block of code until the specified condition is met |
| − | * | + | * The loop keyword creates an infinite loop unless you explicitly break out of it |
| − | * | + | * Rust provides several ways to loop, including '''loop''', '''while''', and '''for''' |
| − | |- | + | |- |
|| | || | ||
|| Let us see an example for a '''loop''' program. | || Let us see an example for a '''loop''' program. | ||
| − | Clear the code window and replace the code from the | + | Clear the code window and replace the code from the Code file as shown. |
| − | |- | + | |- |
|| fn main() { | || fn main() { | ||
| Line 163: | Line 161: | ||
If there is no if statement, it will execute infinitely. | If there is no if statement, it will execute infinitely. | ||
| − | Press ''' | + | Press '''Ctrl and S '''to save the file. |
Let us run the program. | Let us run the program. | ||
| − | |- | + | |- |
|| | || | ||
|| In the terminal, type '''cargo run''' | || In the terminal, type '''cargo run''' | ||
| Line 173: | Line 171: | ||
It stops executing when the '''count''' reaches 5. | It stops executing when the '''count''' reaches 5. | ||
| − | |- | + | |- |
|| | || | ||
|| Next we will see how the '''while''' loop works. | || Next we will see how the '''while''' loop works. | ||
| − | Replace the code from the | + | Replace the code from the code file with the '''while''' loop program. |
| − | |- | + | |- |
|| fn main() { | || fn main() { | ||
| Line 202: | Line 200: | ||
After the 5th iteration, the value of the '''counter''' will be 6. | After the 5th iteration, the value of the '''counter''' will be 6. | ||
| − | So the condition, '''counter | + | So the condition, '''counter < 6''' becomes false and the loop is terminated. |
Save the program. | Save the program. | ||
| − | |- | + | |- |
|| | || | ||
|| In the terminal, type '''cargo run''' | || In the terminal, type '''cargo run''' | ||
Check the output. | Check the output. | ||
| − | |- | + | |- |
|| | || | ||
| − | || | + | || Next we will see about '''For''' loop. |
| − | |- | + | |- |
|| '''Slide 7''' | || '''Slide 7''' | ||
| Line 223: | Line 221: | ||
* In a '''For''' loop, many collections like arrays, vectors, and ranges, implement the '''Iterator''' | * In a '''For''' loop, many collections like arrays, vectors, and ranges, implement the '''Iterator''' | ||
| − | |- | + | |- |
|| '''Slide 8''' | || '''Slide 8''' | ||
| Line 239: | Line 237: | ||
A range with two dots like 0..11 is inclusive on the left. | A range with two dots like 0..11 is inclusive on the left. | ||
| − | + | That is it starts at 0 and exclusive on the right. That is ends at 10 | |
This program will print numbers from 0 to 10. | This program will print numbers from 0 to 10. | ||
| − | |- | + | |- |
|| | || | ||
|| Next let us see an example of a for loop with an iterator method. | || Next let us see an example of a for loop with an iterator method. | ||
| − | Clear the code window and copy and paste the code from the | + | Clear the code window and copy and paste the code from the Code file. |
| − | |- | + | |- |
|| fn main() { | || fn main() { | ||
| Line 261: | Line 259: | ||
|| In this code, we have an array '''‘data’''' containing five integers. | || In this code, we have an array '''‘data’''' containing five integers. | ||
| − | The '''data.iter() '''method creates an iterator for the array '''data'''. | + | The '''data.iter() ''' method creates an iterator for the array '''data'''. |
The '''for''' loop takes each element produced by the iterator and binds it to the variable '''data'''. | The '''for''' loop takes each element produced by the iterator and binds it to the variable '''data'''. | ||
| − | Inside the for loop, '''println''' is used to print the value of each element. | + | Inside the '''for''' loop, '''println''' is used to print the value of each element. |
Save the file. | Save the file. | ||
Let us execute the program. | Let us execute the program. | ||
| − | |- | + | |- |
| − | || | + | || In the terminal, type '''cargo run'''. |
| − | || In the terminal, type '''cargo run''' | + | || In the terminal, type '''cargo run'''. |
| − | We can see the array elements are printed in sequence. | + | We can see the array elements are printed in the sequence. |
| − | |- | + | |- |
|| | || | ||
|| We will learn some more concepts that work with a '''for''' loop. | || We will learn some more concepts that work with a '''for''' loop. | ||
| − | Let us see how to use a '''reverse''' method in the for loop. | + | Let us see how to use a '''reverse''' method in the '''for''' loop. |
| − | |- | + | |- |
|| '''fn''' main() { | || '''fn''' main() { | ||
| Line 293: | Line 291: | ||
|| Copy and paste the code from the code file. | || Copy and paste the code from the code file. | ||
| − | + | If we want the range to include 11 as well, we can write it as '''(1..<nowiki>=11).</nowiki>''' | |
This is known as an inclusive range. | This is known as an inclusive range. | ||
| Line 299: | Line 297: | ||
The '''rev() '''method is applied to the range to produce a reverse iterator. | The '''rev() '''method is applied to the range to produce a reverse iterator. | ||
| − | This means the | + | This means the number will be fetched in reverse order. |
Save the file. | Save the file. | ||
Let us check the output. | Let us check the output. | ||
| − | |- | + | |- |
|| | || | ||
|| In the terminal, type '''cargo run''' | || In the terminal, type '''cargo run''' | ||
We can see the output displayed in the reverse order as expected. | We can see the output displayed in the reverse order as expected. | ||
| − | |- | + | |- |
|| '''Slide 11''' | || '''Slide 11''' | ||
| Line 315: | Line 313: | ||
Let us summarize. | Let us summarize. | ||
| − | |- | + | |- |
|| '''Slide 12''' | || '''Slide 12''' | ||
| Line 322: | Line 320: | ||
Write a program to print odd numbers between 11 and 1 using a for loop | Write a program to print odd numbers between 11 and 1 using a for loop | ||
| − | *Hint: use Rev() method | + | * Hint: use Rev() method |
| − | |- | + | |- |
|| '''Slide 18''' | || '''Slide 18''' | ||
| − | | | + | ||Thank you for joining. |
|- | |- | ||
|} | |} | ||
| − | |||
Latest revision as of 15:07, 12 August 2025
| Visual Cue | Narration |
| Slide 1 | Welcome to the Spoken Tutorial on Control Flow in Rust. |
| Slide 2
Learning Objectives |
In this tutorial, we will learn about
|
| Slide 3
System Requirements |
This tutorial is recorded using
|
| Slide 4
Prerequisites |
To follow this tutorial,
|
| Slide 5
Code Files |
|
| We will see about conditional statements in Rust. | |
| Slide 6
Conditional Statements |
Rust supports
statements for making decisions based on conditions. It always returns a boolean value. |
| Open Visual code editor | Let us open the visual code editor and understand the control statements with examples. |
| In the menu bar, click on Terminal and select New Terminal
We can see a terminal window at the bottom. | |
| > cd MyRustProject
> cargo new functions |
Go to our working directory MyRustProject as explained earlier.
Type the command cargo new controlflow and press Enter Open the created project as shown. |
| In the main.rs file, copy and paste the code from the code file. | |
| fn main() {
let n = 100; if n>100{ println!("Greater than 100"); }else if n==100{ println!("Equal to 100"); }else { println!("Smaller than 100"); } } |
Let us look at this example for an if else statement.
If n is greater than 100, it prints a “greater than 100” statement. If n equals 100, the second print statement will be executed. If n is less than 100, it prints “smaller than 100”. You can include multiple else if statements to check multiple conditions in sequence. The condition must be true to run the code wrapped to it. Press Ctrl and S to save the file. Now let us run the program. |
| In the menu bar, click on Terminal and select New Terminal. | In the menu bar, click on Terminal and select New Terminal. |
| > cargo run | In the terminal, type cargo run to see the output.
The output shows “equal to 100” as we assigned n as 100 in the program. |
| Next let us see loop statements in Rust. | |
| Slide
Loop |
|
| Let us see an example for a loop program.
Clear the code window and replace the code from the Code file as shown. | |
| fn main() {
let mut count = 0; loop { count += 1; println!("Count: {}", count); if count == 5 { break; } } } |
This example has a mutable variable named count which is assigned to the value 0.
A loop is created in which it increases the count variable by 1. It prints the value of count in the current iteration. It will also check for the if statement condition. When the variable count will be equal to 5, it will stop executing and come out of the loop. If there is no if statement, it will execute infinitely. Press Ctrl and S to save the file. Let us run the program. |
| In the terminal, type cargo run
We can see the count variable is printed from 1 to 5. It stops executing when the count reaches 5. | |
| Next we will see how the while loop works.
Replace the code from the code file with the while loop program. | |
| fn main() {
let mut counter = 1; // usage of while loop while counter < 6 { println!("{}", counter); counter += 1; } } |
The while loop runs as long as a condition is true.
Here, the loop keeps running till the counter variable is less than 6. Inside the loop, we are increasing the value of the counter by 1. After the 5th iteration, the value of the counter will be 6. So the condition, counter < 6 becomes false and the loop is terminated. Save the program. |
| In the terminal, type cargo run
Check the output. | |
| Next we will see about For loop. | |
| Slide 7
For loop |
|
| Slide 8
For loop -example fn main() { for n in 0..11 { println!("{}", n); } } |
The For loop has a definite start and endpoint with increment for each iteration.
A range with two dots like 0..11 is inclusive on the left. That is it starts at 0 and exclusive on the right. That is ends at 10 This program will print numbers from 0 to 10. |
| Next let us see an example of a for loop with an iterator method.
Clear the code window and copy and paste the code from the Code file. | |
| fn main() {
let data = [2, 1, 17, 99, 34, 56]; for i in data.iter(){ println!("{}", i); } } |
In this code, we have an array ‘data’ containing five integers.
The data.iter() method creates an iterator for the array data. The for loop takes each element produced by the iterator and binds it to the variable data. Inside the for loop, println is used to print the value of each element. Save the file. Let us execute the program. |
| In the terminal, type cargo run. | In the terminal, type cargo run.
We can see the array elements are printed in the sequence. |
| We will learn some more concepts that work with a for loop.
Let us see how to use a reverse method in the for loop. | |
| fn main() {
for i in (1..=11).rev() { println!("{i}..."); } println!("Launch!"); } |
Copy and paste the code from the code file.
If we want the range to include 11 as well, we can write it as (1..=11). This is known as an inclusive range. The rev() method is applied to the range to produce a reverse iterator. This means the number will be fetched in reverse order. Save the file. Let us check the output. |
| In the terminal, type cargo run
We can see the output displayed in the reverse order as expected. | |
| Slide 11 | This brings us to the end of this tutorial.
Let us summarize. |
| Slide 12
Assignment |
As an Assignment, do the following:
Write a program to print odd numbers between 11 and 1 using a for loop
|
| Slide 18 | Thank you for joining. |