Fri. Dec 6th, 2019

Host your Website

Read and learn

FatCow Web Hosting $1.00/mo* Trust your web hosting to the #1 web host provider, GoDaddy!

For loop in JavaScript

2 min read



Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspot.com/2014/12/for-loop-in-javascript.html

In this video we will discuss for loop in JavaScript with an example.

To understand for loop, let us print even numbers starting from ZERO till a number provided by the user at runtime.

First, let us do this using a while loop. The following while loop example prints all even numbers starting from ZERO till a number provided by the user at runtime. For example if the end user provides 10 at runtime, even numbers starting from ZERO till 10 are printed.

var targetNumber = Number(prompt(“Please enter your target number”, “”));
var start = 0;
while (start [= targetNumber)
{
document.write(start + “[br/]”);
start = start + 2;
}

With while loop
1. The initialization of the variable is done at one place
2. Boolean condition is checked at another place
3. The variable participating in the boolean expression is updated at another place

For loop example : With for loop all three can be done in one place. To make this clear, look at the syntax of the for loop below.
for(initialization; boolean Condition; update variable)
{
statements;
}

Now, let us rewrite the above example using a for loop, instead of while loop

var targetNumber = Number(prompt(“Please enter your target number”, “”));
for (var start = 0; start [= targetNumber; start = start + 2)
{
document.write(start + “[br/]”);
}

Notice that all the 3 things, i.e
1. Variable initialization
2. Boolean condition check and
3. Updating the variable participating in the boolean expression

are all done at one place in the for loop. All these 3 things are optional in a for loop.

Example : Variable intialization is optional in a for loop. Notice that we have moved variable intialization (var start = 0;) outside of the for loop head.

var targetNumber = Number(prompt(“Please enter your target number”, “”));
var start = 0;
for (; start [= targetNumber; start = start + 2)
{
document.write(start + “[br/]”);
}

Example : Just like variable intialization, condition check is also optional in a for loop. Notice that we have moved condition check (start ] targetNumber) from the head of the for loop, into it’s body and we used a break statement to end the loop, otherwise this could become an infinite for loop.

var targetNumber = Number(prompt(“Please enter your target number”, “”));
var start = 0
for (; ; start = start + 2)
{
if (start ] targetNumber)
{
break;
}
document.write(start + “[br/]”);
}

Example : In this example we have moved the expression that updates the variable participating in the boolean expression (start = start + 2) from the head of the for loop, into it’s body.

var targetNumber = Number(prompt(“Please enter your target number”, “”));
var start = 0
for (; ; )
{
if (start ] targetNumber)
{
break;
}
document.write(start + “[br/]”);
start = start + 2;
}

24 thoughts on “For loop in JavaScript

  1. Hi sir , please upload a video on forEach loop this is array prototype method its very simple , Also please tells about forEach how it's take value of array and iterate it

  2. Wowwwwwwwwwwwwwwwwwwwwww venkat Sir Come back After 3 Week…keep Continue  venkat Sir………………………………..we are waiting for Learning from Venkat Sir ..

  3. Legend is back….. with his exceptional way of teaching and presenting the topics which is useful for every dot net developer. Hats of to his service for the community. From USA its thenewboston and from India its kudvenkat

  4. respected sir .plz upload more video as possible and also we need jquery in asp.net because sir jquery is much more in demand and  we need trainner like you who can teach very effectively to used it in pages.plz sir do fast we  eagerly waiting for video.thanx and regards

  5. I already thought that you left your hobby, but nice to see you again ) Thanks for your work and i have still one question, will you do some jquery tutorials about things which are popular at the moment like how to use a plug-ins, for example date-picker or all in one about new things which could be helpful in asp.net development ? =)

Comments are closed.

Copyright © All rights reserved. | Newsphere by AF themes.