is in duration
in and the word loop are the same thing
an example you
to write some code a few statements to deal with for example
changing the position open image on the screen so you write a few statements
once
and then you figured out you have to do it again so you write again
and again and again
as you can see it can get pretty tedious so rather to write it out several times
the same thing
you will like to rate your statements once and just say
repeat less repeated five times or repeated five thousand times
or keep repeating until you told it to stop
or go through all the fields on a webpage or go through every mp3 file in
a folder
so we might repeat two statements we might repeat a dozen
or a 1000
this is what we do by creating loops and loops is also known as iteration
now creating loops is very easy is not when we create a loop
is when to stop a loop
if we need to loop five hundred times who's keeping track
if we need to loop until he told to stop so where's the code to figure it out
when to stop
so all our loop tester have what is called conditions
they control how long the loop
let's see how we do it
we have see already the if statement if the condition is true
do whatever is in the block and in this case we have a variable
a equal to 1 if they is less than 10
we pop up an alert box classic if statement here
now simple replace the word if with the word
while and we have a loop not if this condition is true
but while this condition is true this could have
one line of code in this block or one hundred lines
and whatever is in the body of the loop get executed for iteration
meaning every time loop goes around so here what's happen
we set the variable a to equal to 1
and we store that value in memory we then jump down to the while
and we check the condition just like the if statement
and evaluators true yes a
is less than 10 so next we jump into the body of the loop
inside the curly braces and we executed whatever code is there
in this case just popping out an alert box then we get to the end up the loop
the N double loop is marked by the closing curly braces up the while
statement
now what happened here is we jumped back to the start of the loop
and we check the condition again this happens automatic
is a less than 10 yes it is so we move on
and we go on again
and here is the problem
if nothing in this block changes the value of day
andy is what we are checking
we will now have an infinite loop and an infinite loop is a very bad thing
we will be stock on this loop for ever or at least
until we turn our computer of or force our program to terminate
so we need to add a line to increment the variable a
so if we add a a plus plus inside the body of the loop
then we go around again in will check to pay is less than 10
yes it is we pop up the alert rocks
and right before the body of the loop closes we say
a plus plus meaning we add one to the value of day
a is now up to you we come back up and we will check it again
if Ray is less than 10 yes it is and we keep going
incremental value of day but in some point a will be 10
then we ask it again is a less than 10
no it isn't that is not less than 10 it is 10
so we'll count is false
then we will jump out of the loop right to the end
and we move on to execute whatever code comes after
so this while loop is the most basic while loop
there is in javascript and it's common in many many languages
and takes this general form it's almost identical in lot of in other languages
but there are other kinda bloops has we'll see in the next video