for. what does it stand for?

for(value = 0 ; value <= 255; value+=5) // fade in (from min to max)
what does the "for" mean? i was wondering if someone can make it so easy to understand like how ladyada explained "if" and "else"
Alcreion1

Posts: 7
Joined: Fri Jan 23, 2009 10:05 pm

Re: for. what does it stand for?

"For each value in the range defined by"( initialization ; <continuation conditional> ; upkeep )

The 'initialization' term is executed once at the very beginning. The loop keeps running until the '<continuation conditional>' evaluates as false. The Upkeep is executed once per pass at the very end.

So:

for(int i=0; i<100; i++) // This code declares a new integer variable i and sets it to zero. As long as I is less than 100 it will keep running. Add one to i and save it after every loop.
Fjornir

Posts: 58
Joined: Fri Nov 28, 2008 3:14 pm

Re: for. what does it stand for?

"For" is a type of "loop" construct. A loop is simply a concept that allows something in our code to happen over and over again under some condition. The "for" loop construct has some variable that is initialized to a value and either increments or decrements each time the loop is executed--to a target value.

The "for" statement has three parts. The first initializes a variable to some value. The second represents some condition that must be true for the loop to be executed; once the condition is false, we are out of the loop. The third represents a change to the variable after each loop.

Code: Select all | TOGGLE FULL SIZE
for (value=0; value<=255; value+=5) // fade in (from min to max)

In this "for" loop, value is a variable (it can change). It is initialized to 0. So long as value is less than or equal to 255, then the loop will continue to execute. After each iteration of the loop, value increments by 5 (value +=5 means value = value + 5). So this loop will execute 52 times: if value starts at 0 and is incremented by 5 after each execution of the loop, then it will take 52 times before it reaches 255. On the 53rd time, it will reach 260 (which is not less than 255), so we will break out of the loop.

Does this make sense?

Here's a simpler example. Suppose I wanted to execute the same thing 10 times:

Code: Select all | TOGGLE FULL SIZE
for (i=0; i<10; i=i+1)

Typically, we write it as follows:

Code: Select all | TOGGLE FULL SIZE
for (i=0; i<10; i++)

This is because "i = i + 1" is the same thing as "i += 1" which is the same thing as "i++".
Niksun

If you thought before that science was certain--well, that is just an error on your part. -- Richard Feynman

niksun

Posts: 202
Joined: Sun Jul 27, 2008 9:59 pm

Re: for. what does it stand for?

niksun wrote:"For" is a type of "loop" construct. A loop is simply a concept that allows something in our code to happen over and over again under some condition. The "for" loop construct has some variable that is initialized to a value and either increments or decrements each time the loop is executed--to a target value.

The "for" statement has three parts. The first initializes a variable to some value. The second represents some condition that must be true for the loop to be executed; once the condition is false, we are out of the loop. The third represents a change to the variable after each loop.

Code: Select all | TOGGLE FULL SIZE
for (value=0; value<=255; value+=5) // fade in (from min to max)

In this "for" loop, value is a variable (it can change). It is initialized to 0. So long as value is less than or equal to 255, then the loop will continue to execute. After each iteration of the loop, value increments by 5 (value +=5 means value = value + 5). So this loop will execute 52 times: if value starts at 0 and is incremented by 5 after each execution of the loop, then it will take 52 times before it reaches 255. On the 53rd time, it will reach 260 (which is not less than 255), so we will break out of the loop.

Does this make sense?

Here's a simpler example. Suppose I wanted to execute the same thing 10 times:

Code: Select all | TOGGLE FULL SIZE
for (i=0; i<10; i=i+1)

Typically, we write it as follows:

Code: Select all | TOGGLE FULL SIZE
for (i=0; i<10; i++)

This is because "i = i + 1" is the same thing as "i += 1" which is the same thing as "i++".

ic so the example i put made something for example an LED to slowly fade by 5s?
Alcreion1

Posts: 7
Joined: Fri Jan 23, 2009 10:05 pm

Re: for. what does it stand for?

Well, that's a hard thing to know. It all depends on what "value" is "connected" to and what your circuit looks like. Fading an LED can be done in several ways though. One involves supplying it with less current, but it has a lower threshold (which means it doesn't completely fade out slowly); it will fade and at some point (once the supply voltage is less than its voltage drop) turn off abruptly. Then there are other things to consider such as series resistors and such if they exist. A better way to "fade" an LED is to use pulse width modulation.

But anyways. I am supposing that "value" is a byte. Initially, it's 0 which is 00000000 in binary; and 255 is 11111111 in binary. I'm not sure what value is "connected" to on the Arduino, but it is increasing by 5 each time. This could represent some sort of voltage scale, but I'm not sure. It all depends on your circuit.
Niksun

If you thought before that science was certain--well, that is just an error on your part. -- Richard Feynman

niksun

Posts: 202
Joined: Sun Jul 27, 2008 9:59 pm