function
forloop(){
tb="TEACH ME";
tb_new="";
count_letters=0;
for (i=0,i<tb.length,i++){
tb_new = tb_new + tb(i);
count_letters=eval(count_letters)+1;
} // end for
} //
end function
loop can have in it 1 or more instractions
to execute. the "{"
tell us that it's the start of the loop,
and "}" means end of loop.
the instruction/s between the two sign
"{" "}" will be executed
when the loop is active...
for (i=0,i<tb.length,i++)
i=0
this tells the loop to go on, where "i"
will get the value of "0"
at the first time.
i<tb.length
this tell
the loop to keep going while i is less then the length of
the string tb. in
our case, the length is 7 :
TEACH ME
01234567 --> REMEMBER,
COUNT STARTS FROM 0 !!!
i++
this
tell the loop to increase i by 1 each time the loop is active.
click here to execute the loop |
tb="TEACH
ME';
tb_new="";
count_letters=0;
for (i=0,i<tb.length,i++){
tb_new = tb_new
+ tb(i);
count_letters=eval(count_letters)+1;
let's look how this loop will do what we want :
LOOP 1 :
- when it starts i
will be = 0
- tb_new
= tb_new + tb(i) = put in tb_new what in it allready +
what is in tb(0). in tb_new there
is nothig yet (tb_new="").
in tb(0) is the letter "T", so after
this instruction, tb_new="T".
- count_letters=eval(count_letters)+1
= put in count_letter the
evaluation
of what in it allready + 1, it has 0 in it, so after this
instruction
count_letters = 1.
Now
comes the question if we need to keep
on looping :
Is i <
length of tb ? is (i < 7) ?? yes, i=0, so next loop...
i was 0, so now do the i++ increase i by 1 and do the next loop..
LOOP 2 :
- when it starts i
will be = 1
- tb_new
= tb_new + tb(i) = put in tb_new what in it allready +
what is in tb(1). in tb_new there
is T .
in tb(1) is the letter "E", so after
this instruction, tb_new="TE".
- count_letters=eval(count_letters)+1
= put in count_letter the
evaluation
of what in it allready + 1, it has 1 in it so after this
instruction
count_letters = 2.
Now
comes the question if we need to keep
on looping :
Is i <
length of tb ? is (i < 7) ?? yes, i=1, so next loop...
i was 1, so now do the i++ increase i by 1 and do the next loop..
LOOP 3 utill LOOP 7 works the same way....
LOOP 8 :
- when it starts i
will be = 7
- tb_new
= tb_new + tb(i) = put in tb_new what in it allready +
what is in tb(7). in tb_new there
is "TEACH M".
in tb(7) is the letter "E", so after
this instruction,
tb_new="TEATC ME". That
is what we want it to be !!!!
- count_letters=eval(count_letters)+1
= put in count_letter the
evaluation
of what in it allready + 1, it has 7 in it so after this
instruction
count_letters = 8.
Now
comes the question if we need to keep
on looping :
Is i <
length of tb ? is (i < 7) ?? NO, i=7, so, no more loops...
OK, we used
this loop for :
- transfer
the value of tb to tb_new char by char.
- to count
the letters being transfered.
-----------------------------------------------------------
We can force
the loop to stop working before its normal condition.
We do it by
set the value of "i" (in the example above) to the
final value
for this loop, in our case it was "7".
This is usfull
when we look during the loop for some condition to
become true,
and we don't want the loop to keep going.
example :
...
...
for (i=1,i<7,i++){
if (x==y){
z=z+y;
i=7; <--- here we force the loop to finish
looping
{
even we are in our 3rd loop....
{
...
-----------------------------------------------------------
NOTE : in our
case, we called the "loop count var" "i", but it can be
what ever you like : x abc kuku etc....
it can start from any starting value, not need to be 0....
the loop don't
need to use the count var (in our case "i") in the
loop, loop
can serve us just for counting loops :
count=0
for
(x=5,i<10,x++){
count = eval(count+2);
alert("count=
" +count);
}
this loop starts from x=5, add 2
to "count", alert count, increase
x by 1:
x=5
x=6 x=7
x=8 x=9
x=10
count=2 count=4
count=6 count=8 count=10 count=12
see, we didn't use x in the loop,
just to count the 5 times that
we want the loop to execute...
I hope that this was helpfull...
The snake