So what are For Loops and why should someone use it :
The definition will tell us both the things about what are for loops and why do we need them.
Suppose you want to print number one to 100 so you won'tlove to write:-
So what if there is a way to write a code of few lines to make it all work...?
Interesting right...? Well let's see how can you do that.
Step By Step Demonstartion on how to use For Loops:
Step 1 : create a html structure just like all the other tutorials.
Step 2 : Create following script tag and write the code as it is.
Step 3 : Run your file with the browser and press F12 to see the result.
Full Code :
Further Explanation :
In the Above code when we have used for loops what we are doing is :-
- first we initialiaze i = 0 .
- second we compare if it's less than 3 ( 0 is less than 3 so the condition here is true so the code in brackets will execute).
- lastly we increment i.e we increase the vale of i by 1 and repeat what we do in step 2 i.e we compare if i is less than 3 again and we continue to execute the code until the condition become false.
- So when i is 0 we have console.log ---> console.log("Person at rank " + (i+1) + " is " + blurtpower[i]);
It will print that Person at rank 1 is @double-u as value of blurtpower[i] for the first case is @double-u.
Similarly for i = 1 the console will print Person at rank 2 is @sct.blurt.
i = 2 the console will print Person at rank 3 is @tomoyan.
When i becomes 3 then condition becomes false as 3 is not less than 3.
That's all about the tutorial.
Ask Your Questions below.
Links to all the tutorials are shared below