Go Down

Topic: Does for reset itself? (Read 490 times) previous topic - next topic


Lets say I make a for command that increments a number untill its 255.
After this is finished, will the for reset? or should you manually reset?


Jul 06, 2009, 12:43 am Last Edit: Jul 06, 2009, 12:43 am by Kitep Reason: 1
I don't understand the question.

If you have
Code: [Select]
int i;
for (i=0;i<=255;i++) {
 // do stuff

then i==256 when you exit the loop.

Are you perhaps thinking about bytes, which can only hold 0-255?  If you have
Code: [Select]
byte i;
for (i=0;i<=255;i++) {
 // do stuff

then since i is a byte, it can only go from 0-255.  In this case when i++ tries to increment i to 256, it will reset to 0.  Since that's <=255, this particular loop will never end.

Good luck!


will the for reset?

It won't reset but it will finish and the instructions following the 'for' will be executed. When all the instructions in the loop() have been done it will run the loop again.


If you are talking about the variable used in your FOR loop, no the value will not reset.

if you used 'X' for ex, after the FOR loop is done, 'X' will contain 255.


Jul 06, 2009, 11:37 am Last Edit: Jul 06, 2009, 11:42 am by vortexnl Reason: 1
So, if i=255, then it would increment one more time. therefor resetting to zero and running the loop again?

But if I use an integer with for, should't I be possible to increment it way more than just 255?

Go Up