Learning Javascript Part VI
Loops and Jumps
So we Learned about Functions in javascript and how useful they are when we want to reuse code.But what if we want to do some thing more often, like what if we want something to happen multiple times.For instance what if we want to print something multiple times on console.Lets say thousand times.Again it would be ridiculous to do it ourselves.So what can helps in this.A loop can this problem and in any programming language they are meant to do it.A loop statement is used when we want to repeat some programming statements until a specified condition is reached. There are basically four types of loops.
1) For Loops
2) While Loop
3) Do- While Loop
The Basic concept of a loop is checking the condition and doing somenthing.
For Loop
Let's first discuss about for loop .The for loop have initialization, condition checking and increment/decrement done very nicely and easily. The Syntax of for loop is shown below.
I know this isn't making much sense to you right now but don't worry soon you will be able to understand it.
So lets say you want write your name to the console 5 times then you can initialize a variable to 0.
Let it be ' i '.
So now we have i = 0;
Now we can give condition, we want it to run 5 times, hence we will give the condition i < 5
Now we want the i to be incremented by one every time the loop runs.This can be done by the increament operator ++. i++;
So what happens when the loop runs every times.
First Time
0 < 5 Which is trueValue of i will be : 0
It will Print DeeCoder
then come back to check the condition again.
Second Time
1 < 5 Which is true
It will Print DeeCoder
then come back to check the condition again.
Third Time
2 < 5 Which is true
It will Print DeeCoder
then come back to check the condition again.
Fourth Time
3 < 5 Which is true
It will Print DeeCoder
then come back to check the condition again.
Fifth Time
4 < 5 Which is true
It will Print DeeCoder
then come back to check the condition again.
Sixth Time
5 < 5 Which is false
For loop is also called Counting Loop.
While Loop
While loop is muck like For Loop but it on checks condition only in its parenthesis and initialization and increment part is done externally.While loop is called entry controlled loop as it checks the condition Before the Execution.
Syntax of while loop is shown below
The above example can be written in while loop as follows
The above example can be written in do while loop as follows
Thanks for giving your Precious time.Please Comment your thoughts and share the post if you like it.
Learning JavaScript V
Learning Javascript Part IV
Learning Javascript Part III
Learning Javascript Part II
Syntax of while loop is shown below
The above example can be written in while loop as follows
Do while Loop
Do while Loop is similar to the while loop but it is exit controlled loop.It Checks the condtion after the all statements are executed.Which means that this loop will execute at least once irrespective of the condition.Syntax of do while loop is given below.The above example can be written in do while loop as follows
Thanks for giving your Precious time.Please Comment your thoughts and share the post if you like it.
Learning JavaScript V
Learning Javascript Part IV
Learning Javascript Part III
Learning Javascript Part II
Comments
Post a Comment