Difference between revisions of "JavaScript/C2/Loops-in-JS/English"
(Created page with "Title of the script:Loops in JS Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: JavaScript, HTML, for, while, do.. While, for..in {| bor...") |
Nancyvarkey (Talk | contribs) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
Author: Jayesh Katta Ramalingaiah | Author: Jayesh Katta Ramalingaiah | ||
− | Domain Reviewer: | + | Domain Reviewer: Ankita Maske |
− | Novice Reviewer: | + | Novice Reviewer: Praveeen S. |
Keywords: JavaScript, HTML, for, while, do.. While, for..in | Keywords: JavaScript, HTML, for, while, do.. While, for..in | ||
Line 24: | Line 24: | ||
Learning Objectives | Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | |||
− | |||
* Different types of '''Loops''' in '''JS''' | * Different types of '''Loops''' in '''JS''' | ||
* And their usage | * 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 | ||
* '''Visual Studio Code''' version 1.51.1 ('''code editor''') | * '''Visual Studio Code''' version 1.51.1 ('''code editor''') | ||
Line 45: | Line 39: | ||
|| Slide : Pre-requisites | || Slide : Pre-requisites | ||
|| To practice this tutorial, | || To practice this tutorial, | ||
− | |||
− | |||
* You should be familiar with writing and executing''' JS''' files'''.''' | * You should be familiar with writing and executing''' JS''' files'''.''' | ||
* If not, please go through the prerequisite tutorials on this website. | * If not, please go through the prerequisite tutorials on this website. | ||
− | |||
− | |||
|- | |- | ||
Line 56: | Line 46: | ||
|| | || | ||
* The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | * The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | ||
− | * Pls download and extract the | + | * Pls download and extract the files. |
* Make a copy and then use them for practising. | * Make a copy and then use them for practising. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Types of Loops in JS | || Slide: Types of Loops in JS | ||
|| The different '''types''' of '''loops''' are: | || The different '''types''' of '''loops''' are: | ||
− | + | * '''for loop''' | |
− | + | * '''while loop''' | |
− | * ''' | + | * '''do..while loop''' |
− | * ''' | + | * '''for..in loop''' |
− | * ''' | + | |
− | * ''' | + | |
Let us take an example and understand this better. | Let us take an example and understand this better. | ||
|- | |- | ||
− | || Switch to | + | || Switch to the '''Visual Studio Code editor'''. |
− | || Switch to the '''editor | + | || Switch to the '''Visual Studio Code editor'''. |
|- | |- | ||
Line 161: | Line 147: | ||
|| <nowiki>[Firefox] Click on the </nowiki>'''Console''' tab | || <nowiki>[Firefox] Click on the </nowiki>'''Console''' tab | ||
|| Now click on the '''console tab''' in the '''developer tools.''' | || Now click on the '''console tab''' in the '''developer tools.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 271: | Line 254: | ||
− | Now when the '''<nowiki>condition i | + | Now when the '''<nowiki>condition i less than </nowiki>5''' is checked, it will '''return false.''' |
Line 281: | Line 264: | ||
− | + | Now, let us learn about '''while loop.''' | |
|- | |- | ||
Line 322: | Line 305: | ||
− | Only if the '''condition | + | Only if the '''condition returns true,''' the '''block '''get '''executed'''. |
− | If the '''condition | + | If the '''condition returns false, '''the '''loop terminates.''' |
|- | |- | ||
Line 349: | Line 332: | ||
|- | |- | ||
− | || Switch to ''' | + | || Switch to '''browser''' |
|| Switch back to the '''browser.''' | || Switch back to the '''browser.''' | ||
Line 373: | Line 356: | ||
− | Now when the '''condition''' '''<nowiki>n | + | Now when the '''condition''' '''<nowiki>n less than </nowiki>5''' is checked, it will return '''false.''' |
Line 379: | Line 362: | ||
|- | |- | ||
− | || Switch to | + | || Switch to editor |
|| Switch back to the '''editor.''' | || Switch back to the '''editor.''' | ||
− | + | Now, let us learn about '''do.. while loop.''' | |
|- | |- | ||
Line 423: | Line 406: | ||
− | So even if the '''condition''' is not satisfied | + | So even if the '''condition''' is not satisfied, the '''block '''gets '''executed '''at least once. |
− | When the '''condition | + | When the '''condition returns false, '''the '''loop terminates.''' |
|- | |- | ||
Line 438: | Line 421: | ||
− | Here also we have used the '''post increment operator''' to do so. | + | Here also we have used the '''post-increment operator''' to do so. |
|- | |- | ||
Line 448: | Line 431: | ||
− | If the '''condition | + | If the '''condition returns true''', the '''loop''' gets '''executed''' again. |
Line 458: | Line 441: | ||
|- | |- | ||
− | || Switch to ''' | + | || Switch to '''browser''' |
|| Switch back to the '''browser.''' | || Switch back to the '''browser.''' | ||
Line 482: | Line 465: | ||
− | Now when the '''condition''' '''<nowiki>m | + | Now when the '''condition''' '''<nowiki>m less than </nowiki>5''' is checked, it will '''return false.''' |
Line 494: | Line 477: | ||
|| To conclude, if the '''condition''' is not satisfied- | || To conclude, if the '''condition''' is not satisfied- | ||
− | * The '''for loop '''and '''while loop''' immediately '''terminates''' and | + | * The '''for loop '''and '''while loop''' immediately '''terminates''' and does not '''execute''' the '''block '''at all |
** It’s also called as '''Entry controlled loop''' | ** It’s also called as '''Entry controlled loop''' | ||
* The '''do..while loop''' will '''execute''' the '''block''' at least once before '''terminating''' | * The '''do..while loop''' will '''execute''' the '''block''' at least once before '''terminating''' | ||
** It’s also called as '''Exit controlled loop''' | ** It’s also called as '''Exit controlled loop''' | ||
− | |||
− | |||
|- | |- | ||
Line 586: | Line 567: | ||
|- | |- | ||
− | || Switch to ''' | + | || Switch to '''browser''' |
|| Switch back to the '''browser.''' | || Switch back to the '''browser.''' | ||
Line 613: | Line 594: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt about: | || In this tutorial, we have learnt about: | ||
− | + | * '''for Loop''' | |
− | + | * '''while Loop''' | |
− | * ''' | + | * '''do..while Loop''' |
− | * ''' | + | * '''for..in Loop '''in '''JS''' |
− | * ''' | + | |
− | * ''' | + | |
− | + | ||
− | + | ||
|- | |- | ||
|| Slide: Assignment | || Slide: Assignment | ||
|| As an assignment: | || As an assignment: | ||
− | |||
− | |||
* Open the file '''assignment.js''' which you have created earlier | * Open the file '''assignment.js''' which you have created earlier | ||
* Clear the existing code | * Clear the existing code | ||
* '''Loop '''the numbers from 1 to 100 | * '''Loop '''the numbers from 1 to 100 | ||
* '''Log''' the values which are divisible by 3 | * '''Log''' the values which are divisible by 3 | ||
− | * Open the file '''MyPage.html '''in a '''web | + | * Open the file '''MyPage.html '''in a '''web browser'''. |
* Observe the output in the '''browser’s console''' | * Observe the output in the '''browser’s console''' | ||
− | |||
− | |||
|- | |- | ||
Line 641: | Line 614: | ||
* 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 649: | Line 620: | ||
* 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:16, 21 June 2021
Title of the script:Loops in JS
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Ankita Maske
Novice Reviewer: Praveeen S.
Keywords: JavaScript, HTML, for, while, do.. While, for..in
|
|
Slide: Title | Hello and Welcome to the spoken tutorial on “Loops in JS”. |
Slide:
Learning Objectives |
In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other browser of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Types of Loops in JS | The different types of loops are:
Let us take an example and understand this better. |
Switch to the Visual Studio Code editor. | Switch to the Visual Studio Code editor. |
[Editor]
Open Folder -> Practice-JS |
In the Welcome page, click on the Open folder link at the top left corner.
|
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named index.html
|
[Editor] Type:
<html lang="en"> <head> <title>Loops</title> </head> <body> <script src="main.js"></script> </body> </html> |
In the index.html file, replace the code as shown.
|
Press: Ctrl + S | Save the file. |
Press: Alt + L and Alt + O
|
Now press the key combinations Alt + L and Alt + O.
|
Show firefox | The default browser will open automatically and a new tab opens. |
[Firefox] Press Ctrl + Shift + I
|
Now press Ctrl + Shift + I keys together.
|
[Firefox] Click on the Console tab | Now click on the console tab in the developer tools. |
Switch to Editor | Switch back to the editor. |
[Editor] Click on
|
In the Explorer panel on the left, under Practice-JS click on the file named main.js |
[Editor] Type:
console.log(i); } |
In the main.js file, replace the code as shown.
|
[Editor] Highlight:
|
Let me explain how the for loop works in JS.
|
[Editor] Highlight:
|
Next, the condition is checked for the value of i = 0.
|
[Editor] Highlight:
|
So, the block of code within the for loop gets executed.
|
[Editor] Highlight:
|
After this we increment i by 1 using i++, which is basically i = i +1.
|
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
1 2 3 4 |
We see the output as 0 1 2 3 4.
|
Switch to Editor | Switch back to the editor.
|
[Editor] Type:
while (n < 5) { console.log(n); n++; } |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
|
Let me explain how the while loop works.
|
[Editor] Highlight:
|
Then comes the while loop.
|
[Editor] Highlight:
|
Here, the block of code gets executed and logs the value. |
[Editor] Highlight:
|
Here, inside the block we have written n++
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
1 2 3 4 |
Now, we see the output as 0 1 2 3 4.
|
Switch to editor | Switch back to the editor.
|
[Editor] Type:
do { console.log(m); m++; } while (m < 5); |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
var m = 0; |
The variable declaration and initialisation happens outside the loop.
We have initialised the variable m to zero. |
Only Narration | This loop is also called an exit controlled loop because the condition check will happen at the end. |
[Editor] Highlight:
|
In do.. while loop, the condition is always checked while exiting the block.
|
[Editor] Highlight:
m++; |
In the loop, we are just logging the value and then increasing it by 1.
|
[Editor] Highlight:
|
This is where the condition check happens.
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
1 2 3 4 |
Now, we see the output as 0 1 2 3 4.
|
Slide: Comparison between loops | To conclude, if the condition is not satisfied-
|
Slide: Iteration over an array | All these three loops can iterate over an array taking the index value as counter. |
Only Narration | Next, we shall learn how to loop over properties of an object.
|
Switch to Editor | Switch back to the editor. |
[Editor] Type:
name1: "Spoken", name2: "Tutorials", };
console.log(obj[key]); } |
In the main.js file, replace the code as shown. |
[Editor] Highlight:
name1: "Spoken", name2: "Tutorials", }; |
Here, we are initializing an object to loop over and print the values in all the object properties. |
[Editor] Highlight:
|
In this loop, we need to create a temporary variable, which holds the property in each and every iteration.
|
[Editor] Highlight:
|
Here, we are logging the value as obj[key]
|
Press: Ctrl + S | Save the file. |
Switch to browser | Switch back to the browser. |
[Browser] [Console Tab]:
Highlight:
Tutorials |
Here, we see in the output that the values for all the properties in the object are printed.
|
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt about:
|
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 (MoE), Government of India |
Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |