var sum = 0;
for (fl=10; fl<=150; fl = fl + 5)
sum = sum + fl;
document.write (sum = sum + fl);
The script is a for loop. The loop parameters are as follows:
1. Declaration & initialisation
2. Logical check
In other words, the 1st parameter of the loop declares and initialises a variable (fl) to 10. The loop then executes the 2nd parameter, which is a logical check. It basically says "if FL is less-than-or-equal-to 150". If this statement is a true statement, it will execute the body of the loop (sum = sum + fl) and will then proceed to the 3rd and final parameter of the loop, which is an increment.
This code can also be written as:
This code doesn't really make much sense, but it may be part of a larger file that we can't see.
var sum = 0;
for (fl=10; fl<=150; fl+= 5)
sum += fl;
document.write (sum += fl);
If you run the script the output would be 2475
for (fl = 10; fl <= 150; fl = fl + 5) This means that you give the value to fl =10 and to add 5 as long as fl value<=150
that means fl = 10,15,20,25.........155
when you have sum=0 that means new sum=0+10, sum=10+15, sum=25+20, sum=55+25
and finally the new sum would be 2475
I think this one ill clearly you question.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)