Edit: Thanks for the help. I'm new to arduino, trying to make "if" and "else" statements. What is wrong with this code?

  int x = 10;
  int RedLed = 2
  int OrangeLed = 3
  int GreenLed = 4
  
void setup() {
  pinMode (RedLed, OUTPUT);
  pinMode (OrangeLed, OUTPUT);
  pinMode (GreenLed, OUTPUT);
  
}

void loop() {


  if (x = 10); {
  digitalWrite (2, HIGH);
  }
  else {
  digitalWrite (3, HIGH);
  delay (1000);
  digitalWrite (4, HIGH);
  delay (100);
  digitalWrite (3, LOW);
  }
}

change to

 if (x == 10) {

= -> instruction
== -> comparison

1 Like

Welcome to the forum

  if (x = 10); 

= is used to assign a value to a variable
== is used to test the value of a variable

Also, the semicolon should not be there as it is the only code that is dependent on the test returning true. The code in the curly brackets that follows it will always be executed

I did not look any further

2 Likes

And also three semicolons missing.

2 Likes

Your code is interesting in that it illustrates some errors that the compiler can & will detect, and some mistakes that it can't.

The missing semicolons here:

You should have received some compiler errors for these - they are straightforward syntax errors.

But you wouldn't have got errors for this:

  if (x = 10); {
  digitalWrite (2, HIGH);
  }

Because the syntax is valid - but what you've written doesn't actually do what you wanted.

You probably received a warning for the if (x = 10) - because that is a very common mistake indeed.

I mention this because it is important to understand that code can compile - ie, have no syntax errors - and yet still not be "correct" for what you wanted it to do.

@hberge you may not have received a warning for some errors.

Because by default the IDE does not report all the warnings the compiler can detect.

Go to the IDE preferences and turn on all warnings and verbosity and so forth. I'm in transit and can't tell you the exact check boxes and settings, but it should be obvious.

Then be sure to heed the warnings which will appear, as do errors, in red ink.

a7

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.