Jump to content

for loop


hisoka

Recommended Posts

for(i=0; i+number; i++)

{

code to be executed;

}

 

for(i=0; i+1; i++)

{

code to be executed;

}

 

1)what does the i+1 means in the for loop ?

 

2)for what purpose we use i+number in the second statement inside the for loop?

 

3)why it executes the code endlessly?

 

4)how to prevent it from executing the code endlessly?

Link to comment
Share on other sites

The part of the loop you're putting in bold is the condition by which the loop decides whether it should continue or not.

 

Any number that is not zero evaluates to true, so as long as i+1 is not zero, the loop will continue to run. The only reason I can see that somebody would put i + 1 there is if i starts off negative. Then, at some point, i will be equal to zero and the loop will stop.

 

Read more about the for() loop here: http://www.w3schools.com/js/js_loop_for.asp

  • Like 1
Link to comment
Share on other sites

Quote

3)why it executes the code endlessly?

Why would it stop? How are you telling it to stop?

 

 

 

What is the difference between this :

<script>var a = new Array(1,2,3,4,5);for(i=0 ; i<5 ; i++){document.write("<br />");document.write(a);}</script>

and this

 

 

<script>var a = new Array(1,2,3,4,5);for(i=0 ; i+5 ; i++){document.write("<br />");document.write(a);}</script>

almost the same . The code to be executed is the same . You can try both of them the first gives (1 2 3 4 5) meanwhile the second loops endlessly until the browser crashes . The only difference is that < is changed to + what provoked that that the loop looped endlessly . My question was WHY it looped endlessly??

Edited by hisoka
Link to comment
Share on other sites

this is an interesting and realistic explanation "Any number that is not zero evaluates to true, so as long as i+1 is not zero, the loop will continue to run"

 

However

 

http://www.w3schools...js_loop_for.asp

 

"If statement 2 returns true, the loop will start over again"

 

for(i=0 ; i<5 ; i++)

 

for(i=0 ; i+5 ; i++)

 

in this case for(i=0 ; i<5 ; i++) i<5 is true because 0<5 is indeed true but the loop did not run endlessly

 

in this case for(i=0 ; i+5 ; i++) i+5 is true because "Any number that is not zero evaluates to true" but the loop run endlessly

 

So why ?

Link to comment
Share on other sites

My question was WHY it looped endlessly??

You should really be able to answer that question yourself. If you can't then keep reading about for loops until you know the answer.

in this case for(i=0 ; i<5 ; i++) i<5 is true because 0<5 is indeed true but the loop did not run endlessly

Is i always 0?You're asking questions without understanding how the code works. It's good that you're experimenting and trying things, but the first thing you need to do is understand how and why the loop works in the first place. Then you'll be able to answer the other questions you're asking.This simple example should answer all of your questions if you understand how it works:
for (var i = 0; i < 5; i++) {  alert("Inside the loop, i is " + i);}alert("Outside the loop, i is " + i);
  • Like 1
Link to comment
Share on other sites

I was very confused at first

 

"My question was WHY it looped endlessly??"

 

the answer to my question , which is given by Foxy Mod is :"Any number that is not zero evaluates to true, so as long as i+1 is not zero, the loop will continue to run" to affirm this :

 

"In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy" falsy is (zero , undefined , NULL.. )

 

1+1 = 2 is always true

 

3+2 = 5 is always true

 

10+25 = 35 is always true

 

and so on ...

 

for (var i = 0; i + 5; i++) i+5 is always true therefore the loop run endlessly

 

However

 

for (var i = 0; i <5; i++)

 

0<5 true

 

1<5 true

 

2<5 true

 

5=5 now i is not <5 so the loop breaks

 

based on that I made some change

 

var a = new Array(1,2,3,4,5,6,7);for(i=0 ; i+7 ; i++){if(a==4 ) // can be 7 or 6 but not more than 7{break;}document.write("<br />");document.write(a);}

now I control the loop

 

as for

 

for (var i = 0; i < 5; i++) {alert("Inside the loop, i is " + i);}alert("Outside the loop, i is " + i);

 

the difference is this : in the first case the code inside the for loop runs 4 time and each time the result is echoed meanwhile in the second case the code runs to the end and when it breaks it assigns the end result to the variable then this latter is echoed

 

 

If I am wrong in all what I wrote , please correct

Edited by hisoka
Link to comment
Share on other sites

I'm still not sure that you quite understand. The for statement has 3 parts. It has an initializer, a run condition, and an "afterthought". The initializer runs exactly once, before the loop starts. The run condition is evaluated every time after the loop runs; if the condition evaluates to true then the loop runs again, and if it evaluates to false then the loop ends. The afterthought runs every time after the loop, before the condition is evaluated. The reason I think you're having trouble with this is because there's not really any reason to write something like "i+5" for the run condition. You might add 5 to i as an afterthought, but there isn't a huge use case (especially for a beginner) to put a math expression as the run condition.How many times would this loop execute:

for (i = 100; i > 0; i -= 10)
If you're using a break statement to break out of a for loop then you're probably using the wrong kind of loop. Break statements are typically used in while loops, not for loops. You use a for loop when you know exactly how many times you need to loop. You use a while loop when you just want to keep looping until something happens.
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
×
×
  • Create New...