Go Down

Topic: Arduino does not understand a ranged FOR loop (Read 6815 times) previous topic - next topic

HazardsMind

The title speaks for itself, but why doesn't it know what a ranged FOR loop is? Is it going to be available in a future update or is it just not important enough to be implemented?

Grumpy_Mike

It is not the arduino it is the GCC compiler, the arduino team just use this compiler.

HazardsMind


CrossRoads

What is a ranged FOR loop? Never heard before.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

HazardsMind

Its an iteration, and sorry, it's called a Range-based for loop

Code: [Select]
int array[10] = {0,1,2,3,4,5,6,7,8,9};

for(int out : array)
{
  Serial.print(out);
}

It should return:  0123456789
It is a feature in C++11, and I thought it would work with the IDE, but nope.

Coding Badly

Is it going to be available in a future update?


You could help move it into a future update.  You are running Windows, correct?

HazardsMind


majenko

FIY it's the same as a "foreach" in other languages.

Coding Badly

#8
May 24, 2013, 11:53 pm Last Edit: Jun 11, 2016, 04:26 am by Coding Badly
yep XP, Vista, and 8
• Ensure the Arduino IDE is not running

• Download this...
http://www.atmel.com/tools/ATMELAVRTOOLCHAINFORWINDOWS.aspx

• Install.  Mine ended up here...
C:\Program Files (x86)\Atmel\AVR Tools\AVR Toolchain\

• Navigate to the following in your Arduino directory...
{ArduinoInstallRoot}\hardware\tools\

• Rename the avr directory to avr (old)

• Create a new directory named avr

• Navigate to the Atmel AVR Toolchain directory

• Select All

• Cut

• Navigate back to the Arduino avr directory

• Paste

• Start the Arduino IDE and test

• Report back what you find

MichaelMeissner


Its an iteration, and sorry, it's called a Range-based for loop

Code: [Select]
int array[10] = {0,1,2,3,4,5,6,7,8,9};

for(int out : array)
{
  Serial.print(out);
}

It should return:  0123456789
It is a feature in C++11, and I thought it would work with the IDE, but nope.

Well, I imagine when the IDE starts distributing a more modern compiler than the 4 year old compiler they are currently distributing, C++11 features will work.  But you can always do it via:

Code: [Select]

int array[10] = {0,1,2,3,4,5,6,7,8,9};

for (size_t i = 0; i < sizeof (array) / sizeof (array[0]); i++)
  {
    int out = array[i];
    Serial.print (out);
  }

majenko


But you can always do it via:

Code: [Select]

int array[10] = {0,1,2,3,4,5,6,7,8,9};

for (size_t i = 0; i < sizeof (array) / sizeof (array[0]); i++)
  {
    int out = array[i];
    Serial.print (out);
  }


Or...

Code: [Select]

int array[10] = {0,1,2,3,4,5,6,7,8,9};
int out;

for (size_t i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
  {
    Serial.print (out);
  }

PaulS

Code: [Select]
for (size_t i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
You sure you got that comma in the right place? Doesn't look right to me.

majenko

#12
May 25, 2013, 02:12 am Last Edit: May 25, 2013, 02:15 am by majenko Reason: 1

Code: [Select]
for (size_t i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
You sure you got that comma in the right place? Doesn't look right to me.

Yup, I'm sure.  It's the "comma operator".  It means "do the left side, then do the right side, and return the value from the right side".

So, that assigns array[ i ] to out, then does the comparison, and returns the result of the comparison to the for loop.

Code: [Select]

matt@laptop01:~$ cat test.c
#include <stdio.h>

void main()
{
int array[10] = {0,1,2,3,4,5,6,7,8,9};
int out;
size_t i;

for (i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
 {
   printf("%d\n", out);
 }
}
matt@laptop01:~$ cc -o test test.c
matt@laptop01:~$ ./test
0
1
2
3
4
5
6
7
8
9
matt@laptop01:~$

HazardsMind

Yea this works.
Code: [Select]
void setup(){
  Serial.begin(9600);
  int array[10] = {
    0,1,2,3,4,5,6,7,8,9  };
  int out;

  for (size_t i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
  {
    Serial.print (out);
  }
}

void loop() {}

Quote
0123456789

majenko

"simple" for loops can be quite complex and convoluted.

Not many people use that syntax for a for loop, as it's not as intuitive as putting the assignment (or using the iterator variable directly) inside the loop itself.

While it's perfectly valid, it's mainly there as a demonstration of how things in C aren't always what they appear.  What you think may be missing in C may just be doable in another way - a not very obvious way.

In general though, for things like that, it's better to be more long winded and do it as specific steps within the loop - that way when you come to look at it again in the morning you know what's going on.

Go Up