Jump to content

logic of adding numbers inside for loop statement


shaijuelayidath
 Share

Recommended Posts

Hello Experts, i would like to know the logic behind the following forloop.

for(i=0;i<5;i++){  
document.write(i+6); // i can't understand whats the logic working here
}

 

the result is: 678910

Actually i was looking for a forloop which is starting from any number and repeating with a condition at certain time. I found the above forloop is doing exact function as i expect. But i can't understand the logic and functional structure happening there. As per my understanding it should not work as the condition mentioend in the parenthesis is "i<5" but i added i in the statement as "i+6" which is greater than 5, so it should get error. But i wonder why it works. And also i am confused on the interrelation between the i of the body of forloop and i inside the parenthesis (). Can anyone explain it in simple theory because i hope understanding this logic is important in further stage of my work. Thanks in advance!

Link to comment
Share on other sites

delete the "+ 6" and you'll see what's happening

Link to comment
Share on other sites

The value stored in i is still less than 6, you are just printing numbers larger than 6 without changing the value of i.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...