i saw this code snippet on another thread and wanted to ask about it, so i guess a new thread is called for(!! ) as it's not relevant to the thread itself.
for(int x = 0; [color=blue][b]c != '/n' &&[/b][/color] x<22; x++) { //The c != /n makes sure request is recieved
c = client.read();
Packet[ x] = c;
Serial.print("RequestChar: ");
Serial.println(c);
}
Your confusing IF construct can be re-written with while to be more explicit:
int x = 0;
char c; // what is this starting as?
while (c != '\n' && x < 22) {
  c = client.read();
  Packet[ x] = c;
  Serial.print("RequestChar: ");
  Serial.println(c);
  x++;
}
It's important to know what 'c' is before the loop though - that must be initialized to something for it to make any sense.
c = client.read();
if (c != '/n') {
Packet[x] = c;
Serial.print("RequestChar: ");
Serial.println(c);
}
}
Close but not quite. Your example will always execute the client.read() statement 22 times. Remember the three things inside the parens of a for loop are just regular statements. The second one should be a statement that evaluates to a logical value, true or false. The loop continues to execute as long as the second statement in the parens is true.
So what the original code does is to terminate the loop after a new line character is received, or after 22 characters are received, whichever comes first.
and just to add something for initialization - this would also be valid code;
I'm worried about the first time c != '/n' is executed. The variable 'c' might not have a value at that moment.
There are many ways to solve that, with an extra flag, or a 'break', or a do-while-loop.
boolean stop = false;
for(int x = 0; x<22 && !stop; x++)
{
 ...
 if( c=='\n')
  stop = true;
}
now to the 'condition' part in blue above; what that does is include a nested if statement after the c = client.read();, right ?
No. You could use an if after the client.read(), but you would have to add something to make it behave the same as it's written: something like this:
for(int x = 0; x<22; x++) {
c = client.read();
if (c != '/n') {
Packet[x] = c;
Serial.print("RequestChar: ");
Serial.println(c);
}
else {
break;
}
}
But the construct as written is quite common. You do see it more often in a while loop, but if you want to keep track of a count so as not to exceed it, for example, you would need to use a variable inside the loop that is explicity incremented. The beauty of using it in a for loop is that you now have an incrementing count and a condition that can change within the loop, both of which can terminate the loop.
retronet_RIMBA1ZO:
ahh yes, i see that now - so that 'nifty feature' allows one to insert a condition to SKIP an iteration.
Actually, a condition to terminate the loop early in this case. I kind of like it. Obviously reading in some input message that has a maximum expected length, so this is just to avoid overrunning the buffer it's being stored in, namely Packet[] in the example.
i haven't tried to understand the code fully, but i understand what you're saying now.
i was thinking of my own possible usage where i loop through, say, the even numbers between 3 and 245 except when they end in '4'.
although i suppose there'd be a mathematical alternative to do that !!
let's just say usage for some String manipulation then.
This mistake crops up in several places in this thread:
if (c != '/n') // Wrong
Although it might compile, it probably isn't doing what you expect. This sequence '/n' represents a multibyte character consisting of a forward slash and an 'n', which is not the same as a newline character. To escape a character literal you need to use a backslash, not a forward slash.
MarkT:
BTW there's no need to limit the for loop to numerical iteration, for instance:
for (Ptr p = &s ; p != null ; p = p->next)
{
}
can be used to iterate a pointer-linked list of structs, not that that is
very common in microcontroller programming.
interesting... am only just getting to grips with libraries and structs and now figuring out how 'extern' is used, not much "newbie" threads for it here, so have to go outside of ardunio.cc - i guess it's really more C++ so wouldn't pop up here unless unexplained.
MarkT:
The comma operator can use used to get more than one increment in the last field
allowing looping with several variables:
for (int i = 0, j = 1 ; i < 8 && j != 10 ; i++ , j += i)
{
..
}
At this point it becomes clear why the "Obfuscated C Contest" exists!
wow, fantastic - one could go wild not being restricted to just nested for loops !!
good tip to know, thanks a lot !