Pages: [1]   Go Down
Author Topic: Does for reset itself?  (Read 460 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't understand the question.

If you have
Code:
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:
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!


« Last Edit: July 05, 2009, 05:43:53 pm by Kitep » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 620
Posts: 33991
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Calgary, AB, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 196
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 42
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So, if i=255, then it would increment one more time. therefor resetting to zero and running the loop again?

EDIT:
But if I use an integer with for, should't I be possible to increment it way more than just 255?
« Last Edit: July 06, 2009, 04:42:12 am by vortexnl » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 620
Posts: 33991
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Yes I don't know why a byte variable was ever introduced it is irrelevant in this discussion.

Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you use and "int" (aka "short") or "long" type, then the "for" loop should terminate correctly.
Logged

Per Arduino ad Astra

Pages: [1]   Go Up
Jump to: