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"
for. what does it stand for?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 58
- Joined: Fri Nov 28, 2008 3:14 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.
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.
-
- Posts: 202
- Joined: Sun Jul 27, 2008 9:59 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.
Let's take your example:
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:
Typically, we write it as follows:
This is because "i = i + 1" is the same thing as "i += 1" which is the same thing as "i++".
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.
Let's take your example:
Code: Select all
for (value=0; value<=255; value+=5) // fade in (from min to max)
Does this make sense?
Here's a simpler example. Suppose I wanted to execute the same thing 10 times:
Code: Select all
for (i=0; i<10; i=i+1)
Code: Select all
for (i=0; i<10; i++)
-
- Posts: 7
- Joined: Fri Jan 23, 2009 10:05 pm
Re: for. what does it stand for?
ic so the example i put made something for example an LED to slowly fade by 5s?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.
Let's take your example:
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.Code: Select all
for (value=0; value<=255; value+=5) // fade in (from min to max)
Does this make sense?
Here's a simpler example. Suppose I wanted to execute the same thing 10 times:
Typically, we write it as follows:Code: Select all
for (i=0; i<10; i=i+1)
This is because "i = i + 1" is the same thing as "i += 1" which is the same thing as "i++".Code: Select all
for (i=0; i<10; i++)
-
- Posts: 202
- Joined: Sun Jul 27, 2008 9:59 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.
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.
Please be positive and constructive with your questions and comments.