moveA = random(0,2);
if (moveA == 0) // IF condition
Serial.print(a0); // First instruction, the code works correctly
strcpy(a3, a0); // when I add this, it won't even upload to Arduino**
else if (moveA == 1) // highlighted as error <Compilation error: 'else' without a previous 'if'>
Serial.print(a1);
strcpy(a4, a1);
else if (moveA == 2)
Serial.print(a2);
strcpy(a5; a2);
delay(200);
** after adding the second instruction under IF, the code doesn't upload to Arduino and an error says that I can't put else if (on the next line) without a previous IF (that is actually two lines before).
this is the error:
Compilation error: 'else' without a previous 'if'
any ideas?
Not quite. First, they are braces {}, not brackets []. Nor parentheses () while we are at it.
And this is fine
if (moveA == 0)
Serial.print(a0); // First instruction, the code works correctly
else if (moveA == 1)
Serial.print(a1);
else if (moveA == 2)
Serial.print(a2);
What required the braces is that when multiple simple statements need to be executed in their entirety or not according to the if condition, you need to make a block statement of them by enclosing them in braces.
I always (ok, nearly always) use braces, even when there's only one line to follow. That way a) I'm in the habit and b) if I go back and add a second line, the braces are already there and I can't forget to add them.