programminghead Logo

JavaScript Loops :

Loops are used in JavaScript to perform a Task Continuously without Declaring That task multiple times. Loops can run a task multiple time or According to Given conditions.
So there 3 Different Loops in JavaScript Like :
1. While Loop,
2. Do while Loop,
3. For Loop.

1. While Loop :

While loop Depends on the given Condition and executes the Declared data inside While loop as long as condition is True.

So when we run our program Compiler will treat our while Loop according to the Given Condition, So if the condition is true then program will execute the Code from while loop and after Executing while loop's code then again Compiler will go back and check the Condition again and if the Given condition is still true then Compiler will execute the Code again until condition return False.
Condition Statement

while Loop JavaScript

While Loop Syntax:


    while(condition){
      statement.....
    }

Printing numbers 1 to 10 :

Without Loop if we want to print Numbers form 1 to 10 then we have to write number 10 times and expert/print that number by using document.write() or other printing methods.
But using While Loop we can print numbers from to 10 by declaring a single print method inside While loop and set while loop condition so we can execute while loops code/statement 10 times.

While Loop Example:

While Loop Example :


<html>
 <head>
  <title> While Loop Example </title>
 </head>
 <body>

   <script type="text/javascript">

    var a=1;

    while(a<=10){
      document.write(a+"<br/>");
      a++;      
    }

   </script>

 </body>
</html>	

Results :

Example Explained :

script tag <script> :

Script tag used inside HTML Document to insert/Declare JavaScript code. and also using this <script;> tag we can include or link External .js file by using src="" attribute.

Variable var:

by using var we declare Variables inside JavaScript file/Code so we can store and call value from that variable name.

while Loop while(condition){code..}

while loop to run our statement multiple times according to given Condition.

while(a<=10):

In while loop's condition we have a<=10 that's mean our loop will run until a is less the 10 or a is equal to 10 (a<=10).

While(){statement..}

In while Loop's Statement we have two Statements

document.write(a+"<br/>")

document.write() method will print the value in the Output screen , where given values are a+"<br/> which mean a will print the value of Variable a and + will concatenate/join our string value ("<br/>) with out variable (a) and "<br/>" will add a line break after every number 1 to 10.

a++ Increment:

Here i have used JavaScript Increment method to increment the Variable a values by 1 every time While loop Executes.

+ Concatenation

this + Concatenation method helps Join string data with variables. in the output method we have used this + Concatenation method to join Variable a value with string <br/> which is a HTML tag to add Break Line.


Do While Loop:

Do While loops works the same as While loop but here we Declare our Statement first inside do and out condition after the Statement in while. As I've already said, Do While works the same as While loop, it will check given Condition first before Executing give Statement.

do while Loop JavaScript

DO While Loop Syntax


    do{
      statement...
      }while(condition);

Do While Loop Example


<html>
 <head>
  <title> Do While Loop Example </title>
 </head>
 <body>

   <script type="text/javascript">

    var a=1;

    do{
      document.write(a+"<br/>");
      a++;      
    }while(a<=5);

   </script>

 </body>
</html>		

Results


For Loop :

For loop Also works the same as other JavaScript Loops like While and Do while loop, but the Declaration part is little Different and handy here.
in other loops we have to declare variables first and the we have to apply increment or Decrement before or after our Statement and in the condition section we can only Define condition like variable is greater or less then, equal, not equal etc.
But in For loop we can Declare variable or assign value, condition (<,<=,>,>=,==) and Increment or Decrement variable at one line.

for Loop JavaScript

for Loop Syntax


    for(init,condition,increment){
      statement.....
    }   

For Loop Example


<html>
 <head>
  <title>For Loop Example </title>
 </head>
 <body>

   <script type="text/javascript">
   
   for(var num=1; num<=5; num++){
   
   document.write(num+"<br/>");

   }

   </script>

 </body>
</html>	

Results

Comments

download minecraft free

http://tinyurl.com/y3klomno
11:35 pm

Hi there! This post could not be written any better! Looking through this article reminds me of my previous roommate! He continually kept talking about this. I most certainly will forward this information to him. Pretty sure he will have a very good read. Thanks for sharing!

download minecraft free

http://tinyurl.com/y2pxz7ab
02:10 am

Howdy! This blog post could not be written much better! Looking at this post reminds me of my previous roommate! He continually kept talking about this. I'll forward this article to him. Pretty sure he's going to have a very good read. I appreciate you for sharing!

download minecraft pc

http://tinyurl.com/y2tx9pcs
04:37 am

Hey there! I'm at work surfing around your blog from my new iphone 4! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the outstanding work!

g

http://tinyurl.com/y38rbfhx
07:23 pm

Do you have a spam problem on this website; I also am a blogger, and I was curious about your situation; many of us have developed some nice practices and we are looking to swap methods with other folks, be sure to shoot me an e-mail if interested.

download minecraft

http://tinyurl.com/y3aj9mvb
01:13 am

I was recommended this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my difficulty. You're incredible! Thanks!

g

http://tinyurl.com/y2edb9t3
06:53 am

Hello! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing several weeks of hard work due to no backup. Do you have any solutions to prevent hackers?

g

http://tinyurl.com/y3fbslel
04:36 am

I don't know if it's just me or if perhaps everybody else encountering problems with your blog. It looks like some of the text on your content are running off the screen. Can someone else please comment and let me know if this is happening to them as well? This could be a issue with my internet browser because I've had this happen before. Cheers

minecraft download free pc

http://tinyurl.com/yyk2xa97
12:03 pm

Your means of describing all in this piece of writing is in fact pleasant, all be able to effortlessly be aware of it, Thanks a lot.

minecraft download free pc

http://t.co/9iLvUiRiTh
03:37 am

I_m not that much of a internet reader to be honest but your sites really nice, keep it up! I'll go ahead and bookmark your site to come back later. Cheers

free minecraft download

http://tinyurl.com/yy5nerzp
07:06 pm

It's going to be ending of mine day, except before finish I am reading this fantastic article to improve my experience.

gamefly

http://tinyurl.com/y693zb9p
09:55 am

Thank you for the good writeup. It in truth was once a entertainment account it. Glance complicated to far added agreeable from you! By the way, how can we keep up a correspondence?

gamefly free trial

http://tinyurl.com/y3mpdse8
02:36 pm

Good day! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I'm getting sick and tired of Wordpress because I've had problems with hackers and I'm looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform.

gamefly

http://tinyurl.com/y4mos6bz
06:36 am

Does your website have a contact page? I'm having a tough time locating it but, I'd like to shoot you an e-mail. I've got some recommendations for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it grow over time.

gamefly free trial

http://bit.ly/2WgriUR
01:04 pm

It's an remarkable piece of writing in support of all the internet viewers; they will obtain benefit from it I am sure.

gamefly free trial

http://j.mp/2WgriUR
07:06 pm

Very energetic article, I enjoyed that a lot. Will there be a part 2?

Write to Us