Difference between revisions of "Rust/C2/Control-Flow-in-Rust/English"
(Created page with "Title of the script: Control-Flow Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: Rust, variables, if, else, else if, conditions {| borde...") |
Nancyvarkey (Talk | contribs) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
Author: Jayesh Katta Ramalingaiah | Author: Jayesh Katta Ramalingaiah | ||
− | Domain Reviewer: | + | Domain Reviewer: Vigneshwer Dhinakaran |
− | Novice Reviewer: | + | Novice Reviewer: Praveen S. |
Keywords: Rust, variables, if, else, else if, conditions | Keywords: Rust, variables, if, else, else if, conditions | ||
Line 23: | Line 23: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about the: | || In this tutorial, we will learn about the: | ||
− | |||
− | |||
* '''if/else control flow statements''' | * '''if/else control flow statements''' | ||
− | * Different '''types''' of '''loops''' | + | * Different '''types''' of '''loops''' supported in '''Rust''' |
− | * '''Syntax''' of all the '''control flow statements '''and their usage | + | * '''Syntax''' of all the '''control flow statements''' and their usage |
− | + | ||
− | + | ||
|- | |- | ||
|| Slide: System Specifications | || Slide: System Specifications | ||
|| This tutorial is recorded using: | || This tutorial is recorded using: | ||
− | |||
− | |||
* '''Ubuntu Linux''' OS version 18.04 | * '''Ubuntu Linux''' OS version 18.04 | ||
* '''Rust''' version 1.47.0 | * '''Rust''' version 1.47.0 | ||
* '''Visual Studio Code''' version 1.45.0 ('''code editor''') | * '''Visual Studio Code''' version 1.45.0 ('''code editor''') | ||
− | However you may use any other '''editor''' of your choice. | + | However, you may use any other '''editor''' of your choice. |
|- | |- | ||
Line 48: | Line 42: | ||
* You should be familiar with '''compiling''' and '''running Rust''' files'''.''' | * You should be familiar with '''compiling''' and '''running Rust''' files'''.''' | ||
* If not, please go through the prerequisite '''Rust''' tutorials on this website. | * If not, please go through the prerequisite '''Rust''' tutorials on this website. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Code files | || Slide: Code files | ||
|| | || | ||
− | * The | + | * The file used in this tutorial is available in the''' Code files''' link on this tutorial page. |
* Pls download and extract the file. | * Pls download and extract the file. | ||
* Make a copy and then use it for practising. | * Make a copy and then use it for practising. | ||
− | |||
− | |||
|- | |- | ||
Line 66: | Line 56: | ||
* This is an integral part of any '''programming language'''. | * This is an integral part of any '''programming language'''. | ||
* This can be achieved using '''if''' and '''else statements''' | * This can be achieved using '''if''' and '''else statements''' | ||
− | |||
− | |||
|- | |- | ||
Line 75: | Line 63: | ||
|| | || | ||
* '''if''' is the most common way to handle the '''flow''' of our code. | * '''if''' is the most common way to handle the '''flow''' of our code. | ||
− | |||
− | |||
|- | |- | ||
Line 84: | Line 70: | ||
|| | || | ||
* We can use '''else if, '''if there are multiple '''conditions''' to be evaluated. | * We can use '''else if, '''if there are multiple '''conditions''' to be evaluated. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Control Flow -1 | || Slide: Control Flow -1 | ||
− | |||
Line 95: | Line 78: | ||
* We can use '''if''' without '''else''', however '''else''' cannot be used without '''if'''. | * We can use '''if''' without '''else''', however '''else''' cannot be used without '''if'''. | ||
* '''else''' is always preceded with an '''if statement'''. | * '''else''' is always preceded with an '''if statement'''. | ||
− | |||
− | |||
|- | |- | ||
Line 135: | Line 116: | ||
<nowiki>[Enter]</nowiki> | <nowiki>[Enter]</nowiki> | ||
− | || Let us create a new '''project''' named ''' | + | || Let us create a new '''project''' named '''control_flow.''' |
Type the '''command''' as shown. | Type the '''command''' as shown. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 148: | Line 126: | ||
− | I will use '''Visual Studio Code editor '''for this demonstration | + | I will use '''Visual Studio Code editor''' for this demonstration. |
|- | |- | ||
Line 165: | Line 143: | ||
− | Then click on the '''OK '''button at the top right corner. | + | Then click on the '''OK''' button at the top right corner. |
|- | |- | ||
Line 171: | Line 149: | ||
Click on '''control_flow''' | Click on '''control_flow''' | ||
− | || Under the '''EXPLORER''' section, expand the '''project''' folder “''' | + | || Under the '''EXPLORER''' section, expand the '''project''' folder “'''control_flow'''” by clicking on it. |
|- | |- | ||
Line 238: | Line 216: | ||
cd '''control_flow'''<nowiki>[Enter]</nowiki> | cd '''control_flow'''<nowiki>[Enter]</nowiki> | ||
− | || Go to the '''project''' folder ''' | + | || Go to the '''project''' folder '''control_flow '''using the '''cd command.''' |
|- | |- | ||
Line 315: | Line 293: | ||
Here, we emphasise that only an '''if condition''' can exist without an '''else statement.''' | Here, we emphasise that only an '''if condition''' can exist without an '''else statement.''' | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
Line 345: | Line 319: | ||
− | This is the '''if- | + | This is the '''if - else if - else statement'''. |
|- | |- | ||
Line 377: | Line 351: | ||
When this '''condition''' is satisfied, the '''else if block''' gets executed. | When this '''condition''' is satisfied, the '''else if block''' gets executed. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 389: | Line 360: | ||
'''}''' | '''}''' | ||
− | || And prints this message as the above two '''conditions''' are not satisfied. | + | || When this '''condition''' also fails, the '''else block '''is executed. |
+ | |||
+ | And prints this message as the above two '''conditions''' are not satisfied. | ||
|- | |- | ||
Line 411: | Line 384: | ||
'''Number is not divisible by 2 and 3''' | '''Number is not divisible by 2 and 3''' | ||
− | || | + | ||'''Number is not divisible by 2 and 3 '''is printed because the above two '''conditions''' are not satisfied. |
− | + | ||
− | + | ||
− | '''Number is not divisible by 2 and 3 '''is printed because the above two '''conditions''' are not satisfied. | + | |
|- | |- | ||
|| Only narration | || Only narration | ||
− | || That’s how the '''if-else if-else statements''' work in '''Rust'''. | + | || That’s how the '''if - else if - else statements''' work in '''Rust'''. |
|- | |- | ||
Line 429: | Line 399: | ||
* '''Loops''' are '''control structures''' that are repeatedly executed until a particular '''condition''' fails. | * '''Loops''' are '''control structures''' that are repeatedly executed until a particular '''condition''' fails. | ||
* That means, '''loops''' repeat a '''block''' of code '''n''' number of times until a particular '''condition '''fails. | * That means, '''loops''' repeat a '''block''' of code '''n''' number of times until a particular '''condition '''fails. | ||
− | |||
− | |||
|- | |- | ||
Line 439: | Line 407: | ||
** '''While loop''' | ** '''While loop''' | ||
* Note: There is no''' do while loop''' in '''Rust''' | * Note: There is no''' do while loop''' in '''Rust''' | ||
− | |||
− | |||
|- | |- | ||
Line 545: | Line 511: | ||
− | This is because the '''loop''' breaks after one execution. | + | This is because the '''loop''' breaks after one '''execution'''. |
Line 591: | Line 557: | ||
'''while a!=0 {''' | '''while a!=0 {''' | ||
− | || After this, the '''while keyword''' is used to initialize a '''while loop.''' | + | || After this, the '''while keyword''' is used to '''initialize''' a '''while loop.''' |
Line 597: | Line 563: | ||
− | Until and unless the '''condition''' returns '''false,''' the '''block''' gets executed. | + | Until and unless the '''condition''' returns '''false,''' the '''block''' gets '''executed'''. |
|- | |- | ||
Line 632: | Line 598: | ||
1 | 1 | ||
− | || So now, we have successfully executed the '''while loop'''. | + | || So now, we have successfully '''executed''' the '''while loop'''. |
− | Only when the value of '''a''' is '''zero''' the '''condition''' returns '''false''' and the '''block''' will stop getting executed. | + | Only when the value of '''a''' is '''zero''' the '''condition''' returns '''false''' and the '''block''' will stop getting '''executed'''. |
Line 670: | Line 636: | ||
'''<nowiki>let arr = [2,3,5,7,11];</nowiki>''' | '''<nowiki>let arr = [2,3,5,7,11];</nowiki>''' | ||
− | || Here we have initialized an '''array''' with few '''number elements''' in it. | + | || Here we have '''initialized''' an '''array''' with few '''number elements''' in it. |
|- | |- | ||
Line 690: | Line 656: | ||
https://doc.rust-lang.org/std/iter/index.html | https://doc.rust-lang.org/std/iter/index.html | ||
− | || '''Rust std''' | + | || '''Rust std module''' provides three forms of iteration. |
− | Here, we are using''' iter() method''' which iterates over | + | Here, we are using''' iter() method''' which iterates over the address of T. |
− | For more details, on '''iter method''' refer to the link. | + | For more details, on '''iter method''' refer to the '''link'''. |
Line 736: | Line 702: | ||
'''Value = 11''' | '''Value = 11''' | ||
− | || The '''loop''' has executed successfully and printed all the values in the '''array.''' | + | || The '''loop''' has '''executed''' successfully and printed all the values in the '''array.''' |
− | We can see the '''for loop''' has been executed once for each and every '''element''' in the '''array'''. | + | We can see the '''for loop''' has been '''executed''' once for each and every '''element''' in the '''array'''. |
− | And on reaching the end of the '''array''', the '''for loop''' stopped executing. | + | And on reaching the end of the '''array''', the '''for loop''' stopped '''executing'''. |
|- | |- | ||
Line 758: | Line 724: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt: | || In this tutorial, we have learnt: | ||
− | |||
− | |||
* '''if/else control flow statements''' | * '''if/else control flow statements''' | ||
* Different '''types''' of '''loops''' and | * Different '''types''' of '''loops''' and | ||
* Their '''syntax''' and usage | * Their '''syntax''' and usage | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Assignment | || Slide: Assignment | ||
|| As an assignment, | || As an assignment, | ||
− | |||
* Go to the project folder '''rust-assignment''' | * Go to the project folder '''rust-assignment''' | ||
* In the '''main.rs''' file | * In the '''main.rs''' file | ||
Line 778: | Line 739: | ||
* '''Compile '''and '''execute '''the project. | * '''Compile '''and '''execute '''the project. | ||
* Observe the output in the '''Terminal''' | * Observe the output in the '''Terminal''' | ||
− | |||
− | |||
|- | |- | ||
Line 786: | Line 745: | ||
* The video at the following link summarises the Spoken Tutorial project. | * The video at the following link summarises the Spoken Tutorial project. | ||
* Please download and watch it | * Please download and watch it | ||
− | |||
− | |||
|- | |- | ||
Line 794: | Line 751: | ||
* We conduct workshops using spoken tutorials and give certificates. | * We conduct workshops using spoken tutorials and give certificates. | ||
* For more details, please write to us. | * For more details, please write to us. | ||
− | |||
− | |||
|- | |- |
Latest revision as of 11:19, 21 June 2021
Title of the script: Control-Flow
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Vigneshwer Dhinakaran
Novice Reviewer: Praveen S.
Keywords: Rust, variables, if, else, else if, conditions
|
|
Slide: Title | Welcome to the spoken tutorial on “Control-Flow in Rust”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about the:
|
Slide: System Specifications | This tutorial is recorded using:
However, you may use any other editor of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Control Flow |
|
Slide: Control Flow
[Highlight]: if expressions |
|
Slide: Control Flow
[Highlight]: else if |
|
Slide: Control Flow -1
|
|
Slide: Control Flow -2 |
Let us understand all this by using an example. |
Press Ctrl+Alt+T keys | Open the terminal by pressing Ctrl,Alt and T keys simultaneously on the keyboard.
|
Only Narration | Here onwards, please remember to press the Enter key after typing each command. |
[Terminal] Type:
[Enter] |
Using cd command go to the Rust practice folder which we created earlier. |
[Terminal] Type:
[Enter] |
Let us create a new project named control_flow.
|
Open Visual Studio Code editor. | You may use any editor of your choice.
|
[Editor]
Open Folder -> control_flow |
Open the created project by clicking on the Open folder link in the Welcome page.
|
[Editor]
Click on control_flow |
Under the EXPLORER section, expand the project folder “control_flow” by clicking on it. |
[Editor] Expand src and click on main.rs | Then expand src and open the main.rs file. |
[Editor] Type:
let a = 30; let b = 20; if a>b{ println!(“A is greater than B”); } else { println!(“B is greater than A”); } } |
In the editor, replace the code as shown. |
[Editor] Highlight:
|
Here we are checking whether the condition a is greater than b or not. |
[Editor] Highlight:
|
When the condition is satisfied, the print statement inside the if block gets printed. |
[Editor] Highlight:
println!(“B is greater than A”); } |
If not, the print statement in the else block gets executed. |
Ctrl + S | Save the file. |
Switch to terminal | Switch to the terminal. |
[Terminal] Type:
|
Go to the project folder control_flow using the cd command. |
[Terminal] Type:
|
Now type cargo run. |
[Terminal] Highlight:
|
The print statement in the if block is executed and printed, as the condition is satisfied. |
Switch to Editor | Switch back to the editor. |
[Editor] Modify:
let marks = 50; if marks>35{ println!(“You are qualified!”); } } |
In the editor, modify the code as shown. |
[Editor] Highlight:
if(marks>35){ println!(“You are qualified!”); } |
Here, in this example, we are considering only the if condition.
|
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project. |
[Terminal] Highlight:
|
As the condition (marks greater than 35) is satisfied, the block gets executed.
|
[Editor] Modify:
fn main(){ let a = 5; if a%2 == 0{ println!(“Number is divisible by 2”); } else if a%3 == 0{ println!(“Number is divisible by 3”); } else { println!(“Number is not divisible by 2 and 3”); } } |
In the editor, let’s update the code as shown.
|
[Editor] Highlight:
println!(“Number is divisible by 2”); } |
Here, we are checking a condition if a % 2 == 0 or not.
|
[Editor] Highlight:
println!(“Number is divisible by 3”); } |
else if block checks the condition if a % 3 == 0 or not.
|
[Editor] Highlight:
else { println!(“Number is not divisible by 2 and 3”); } |
When this condition also fails, the else block is executed.
And prints this message as the above two conditions are not satisfied. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Now type cargo run. |
[Terminal] Highlight:
|
Number is not divisible by 2 and 3 is printed because the above two conditions are not satisfied. |
Only narration | That’s how the if - else if - else statements work in Rust. |
Next, let’s learn about loops. | |
Slide: Control Flow - Loops |
|
Slide: Control Flow - Loops |
|
Only Narration | Let us take an example and understand Loops better. |
[Editor] Type:
loop { println!(“Hello World!”); } } |
Go back to the editor and replace the code as shown. |
[Editor] Highlight:
|
loop is a keyword in Rust which iterates a block until it fails. |
Ctrl + S | Save the file. |
Switch to terminal | Switch to the terminal. |
[Terminal] Type:
|
Now type cargo run. |
[Terminal] Show:
(being printed infinite times) |
We can see Hello World being printed infinite times, until the memory in the stack ends.
|
Only Narration | Press Ctrl + C keys to terminate this loop. |
Switch to Editor | Switch back to the editor. |
[Editor] Modify:
println!(“Hello, World!”); break; } |
In the editor, modify the code as shown. |
[Editor] Highlight:
break; |
Here, we are using break statements to break the execution of the loop. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project. |
[Terminal] Highlight:
|
Notice now, we can see Hello, World printed only once.
|
Only Narration | This is the basic way to loop in Rust.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
let mut a = 3; while a!=0 { println!(“{}”,a); a = a - 1; } } |
In the editor, replace the code as shown. |
[Editor] Highlight:
let mut a = 3; |
Here we have initialized a mutable variable a and assigned the value 3 to it. |
[Editor] Highlight:
|
After this, the while keyword is used to initialize a while loop.
|
[Editor] Highlight:
a = a - 1; |
Here we are printing the number and then reducing the value by 1. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project and see the output. |
[Terminal] Highlight:
2 1 |
So now, we have successfully executed the while loop.
|
Only Narration | Now let us learn about for loop. |
Switch to Editor | Switch back to the editor. |
[Editor] Type:
let arr = [2,3,5,7,11]; for value in arr.iter() { println!(“Value = {}”,value); } } |
In the editor, replace the code as shown. |
[Editor] Highlight:
|
Here we have initialized an array with few number elements in it. |
[Editor] Highlight:
for value in arr.iter() { |
So here value means each and every number element present in the array.
|
[Video Editing]:
Notify: |
Rust std module provides three forms of iteration.
|
[Editor] Highlight:
|
And, in the block we are printing the value for each and every iteration. |
Ctrl + S | Save the file. |
Switch to terminal | Switch back to the terminal. |
[Terminal] Type:
|
Run the project. |
[Terminal] Highlight:
Value = 3 Value = 5 Value = 7 Value = 11 |
The loop has executed successfully and printed all the values in the array.
|
Only narration | That’s how a for loop works in Rust. |
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt:
|
Slide: Assignment | As an assignment,
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Forum questions | Pls post your timed queries in this forum |
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education, Government of India |
Slide: Thanks | This is Jayesh signing off. Thank you for joining |