Jump to content

JavaScript:Tutorial, Loops

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Introduction:-
In this tutorial I will show you the two types of loops there are in JavaScript.

Explanation:-
You might say where is the code? He is already explaining without the code?
Well this time I think that its better if the Explanation comes before the Code

There are 2 types of loops:-
    For Loops
    While Loops

For Loops:-
The For Loops are used in this way

for (something)
{
do something else
}

While Loops:-
The While Loops are used in this way:-

while (something)
{
Do something else
}

Solution:-
Now that you had an idea of the basic usage of the loops here are some practical codes:-

For Loops:-

<SCRIPT LANGUAGE="JavaScript">
var lp=1;
for (lp=0;lp<15;lp++)
{
document.write ("For Loop "+lp+"<BR>");
}
</SCRIPT>
Note: lp is a variable, and until that variable has reached the value of 14 (<15) the loop will go on

While Loops:-

<SCRIPT LANGUAGE="JavaScript">
var lp=0;
while (lp<15)
{
document.write ("While Loop "+lp+"<BR>");
lp++;
}
</SCRIPT>
Note: lp is a variable, and while that variable is <15 the loop will go on

A Preview:-
None

Conclusion:-
As Always Feedback is welcome! In the next tutorial I will tell you how to exit from the loop. I don't think that this needs any source code..

#2
Ronin

Ronin

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 309 posts
Nice tutorial. I need to learn JavaScript sooner or later and these (the ones that you've been posting) will help a lot!

#3
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

Ronin said:

Nice tutorial. I need to learn JavaScript sooner or later and these (the ones that you've been posting) will help a lot!

I'm happy these helped you :) I will be posting more and more.. be sure to see my next tutorial ( will be posted very soon ) on how to break out of a loop... :)

#4
castanza88

castanza88

    Newbie

  • Members
  • Pip
  • 5 posts
good breakdown of code, and examples...wish more people did that...

#5
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts

castanza88 said:

good breakdown of code, and examples...wish more people did that...

Thanks... for your feedback :)