for loop not recognized when placed in sketch

hi, I am doing a simple, led sketch from a tutorial. it works if you copy and stack but it doesn’t turn “for” orange signifying it understands the condition??? the tutorial is from july 2014, has something changed?

at point 39:45 he explains the “for”

the sketch I am using…

 int redLED=10;
 int yelLED=9;
 int grnLED=8;
 int onTime=1000;
 int offTime=1000;
 int numRedBlink=5;


void setup() {
  // put your setup code here, to run once:
 pinMode(redLED,OUTPUT);
 pinMode(yelLED,OUTPUT);
 pinMode(grnLED,OUTPUT);

}

void loop() {

  for (int j=1; j<=numRedBlink; j=j+1); {
  digitalWrite (redLED,HIGH);
  delay(onTime);
  digitalWrite(redLED, LOW);
  delay(offTime);
}
 
  digitalWrite (yelLED,HIGH); 
  delay(onTime);
  digitalWrite (yelLED,LOW);
  delay (offTime);

  digitalWrite (grnLED,HIGH);
  delay (onTime);
  digitalWrite (grnLED,LOW);
  delay(offTime);

}

The colours are just the editor - ignore it they don't do anything worth while.

Mark

Your code does not contain any for loops. It's pointless to ask why code that isn't there does not turn orange.

it has been updated...

  for (int j=1; j<=numRedBlinl; j=j+1);

Properly formatted, this would be:

  for (int j=1; j<=numRedBlinl; j=j+1)
  {
     ;
  }

Not much point in a loop that does nothing, is there?

The code you posted won’t even compile. But, the for statement does turn orange in the 1.05 version of the IDE.

 for (int j=1; j<=numRedBlinl; j=j+1);

Oops

Taa-Daa!

This post marks the bazillionth time someone's been trashed by a dangling semicolon on an if, for, or while that could have been caught had they just used Ctrl-T in the IDE.

ok, here it is copy and pasted NOT edited in reply. it compiles but IGNORES “for” the question is why?

int redLED=10;
int yelLED=9;
int grnLED=8;
int onTime=1000;
int offTime=1000;
int numRedBlink=5;


void setup() {
  // put your setup code here, to run once:
pinMode(redLED,OUTPUT);
pinMode(yelLED,OUTPUT);
pinMode(grnLED,OUTPUT);

}

void loop() {

 for (int j=1; j<=numRedBlink; j=j+1); {
 digitalWrite (redLED,HIGH);
 delay(onTime);
 digitalWrite(redLED, LOW);
 delay(offTime);
}
 digitalWrite (yelLED,HIGH); 
 delay(onTime);
 digitalWrite (yelLED,LOW);
 delay (offTime);

 digitalWrite (grnLED,HIGH);
 delay (onTime);
 digitalWrite (grnLED,LOW);
 delay(offTime);

Have you not read a single reply? It is the semicolon at the end of the for loop. That makes it an empty loop that does nothing. It isnt being ignored, it is doing nothing just like you told it to.

Yes. This is why:

for (int j=1; j<=numRedBlink; j=j+1); {
^
|
|
|
Look though any for loop code and see if you see a semicolon here

thank you! until now I believed everything was to end with ; i will research this. yes i read them, and no i didn't understand. can you believe that, 3 days of tutorials and i still don't know it all. lol

ok it works, don't understand why ; prevented it. OR, why "for" isn't orange

I believed everything was to end with ;

"if", "for", "switch" and "while" (and probably others I forgotten about right now) do not normally end with a semicolon.

can you believe that, 3 days of tutorials and i still don't know it all. lol

I started programming in C in 1977, wrote my first C book in 1982, and I still don't think I know it all.

A semicolon terminates a statement.

A series of statements in braces is called a compound statement and is treated as a single statement.

A for loop calls out for a statement following the parens.

A naked semicolon is a perfectly valid statement.

so, for (something); is perfectly valid, and in fact quite common in pointer based code where the control statement in the for does all the work. For example, strlen() can be written this way.

so, for (something); is perfectly valid, and in fact quite common in pointer based code

Really? I would say that form is pretty rare and, for a beginner, almost always an error. While your comment is clearly correct, I think it may only confuse the OP here since he is just getting started with C. I think we would better serve his needs by saying that form is a common source of errors and let him discover the oddities as he gains more experience.

It shows up all over the place in Harbison and Steele, usually in the name of "Performance".

ETA:
(I just mentioned it because usually the newbie will ask, "Then why doesn't the compiler warn me about the naked semicolon after the for?")

It shows up all over the place in Harbison and Steele, usually in the name of "Performance".

Really? I've read H&S and don't recall one instance where they wrote what the OP wrote:

   for(...); {       // Note the opening brace

But then, that was years ago and I may well have forgotten. Still, sandwiching a semicolon between the closing parenthesis of a for expression and the opening brace of a statement block is something I don't remember in H&S.

Putting the compound braces there is something they would not do. They clearly mark the naked semicolon like this:

struct intlist {struct intlist *link; int data;};

struct intlist *reverse(struct intlist *p)
{
    struct intlist *here, *previous, *next;

    for (here = p, previous = NULL;
          here != NULL;
          next = here->link, here->link - previous,
          previous = here, here = next) /* empty */;
    return previous;
}

Of course, you read Harbison and Steele as a reference, not a style manual. 8^)

Wow! I sure don't remember anything like that. To me, reading that is a fingernails-on-the-chalkboard experience. Also, the third expression in the for loop:

next = here->link, here->link - previous,
          previous = here, here = next) /* empty */ ;

just looks like a train wreck waiting to happen. I would much prefer:

struct intlist {struct intlist *link; int data;};

struct intlist *reverse(struct intlist *p)
{
    struct intlist *here, *previous, *next;

    for (here = p, previous = NULL; here != NULL; next = here->link, here->link - previous, previous = here, here = next)
        ;               /* empty */
    return previous;
}

Question: I know that the order of evaluation on function arguments is not guaranteed. Is the order of evaluation for complex sub-expressions fixed by precedence order or is it also not guaranteed?

econjack:
Wow! I sure don't remember anything like that. To me, reading that is a fingernails-on-the-chalkboard experience. Also, the third expression in the for loop:

next = here->link, here->link - previous,

previous = here, here = next) /* empty */ ;




just looks like a train wreck waiting to happen. I would much prefer:



struct intlist {struct intlist *link; int data;};

struct intlist *reverse(struct intlist *p)
{
    struct intlist *here, *previous, *next;

for (here = p, previous = NULL; here != NULL; next = here->link, here->link - previous, previous = here, here = next)
        ;              /* empty */
    return previous;
}




Question: I know that the order of evaluation on function arguments is not guaranteed. Is the order of evaluation for complex sub-expressions fixed by precedence order or is it also not guaranteed?

No, it's not. Not only that, but this expression:

here->link - previous

does nothing at all, as it does a subtract, the throws away the result!

Correct execution of that code, as written, would be something of a miracle.

Regards,
Ray L.