Expected initializer before 'if'. Please help!! I just started programming today

int led1=6;
int led2=2;
int led3=4;
int led4=3;
int sensor4=9;
int sensor5=10;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(sensor4, INPUT);
pinMode(sensor5, INPUT);
analogRead(sensor4);
analogRead(sensor5);
}
// the set of code above writes for pin6 and pin2; reads for pin9 and pin10.

void loop()

if (sensor4>=4);
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}

else
{
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);

}

I am doing this from my phone so forgive the formatting. Try this and see how it differs from your code.

void loop() 
{
if (sensor4>=4);
{  
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}
 

else
{
  digitalWrite(led2, HIGH);
  digitalWrite(led1, LOW);

}

}
if (sensor4>=4);

If statements that properly end with a semicolon are rarer than unicorns.

if (sensor4>=4); // remove the semi-colon
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}

void loop() 

if

In case you haven't found the original problem, there's a { missing.

It doesn't do much good to read the analog inputs and throw away the results, as these lines do:

analogRead(sensor4);
analogRead(sensor5);

The comparison will always be true, because sensor4 was set to 9:

if (sensor4>=4)

Most people write something like this:

if (analogRead(sensor4) >= 4) {

Expected initializer before

is how the Arduino IDE says "you made an error in some punctuation"

when you see it, verify that:

every { has a matching }
every ( has a matching )
you have an ; everywhere it is needed, and no ; where it is not needed

in re every { has a matching }: highlight any { and the IDE should show you the matching }

Read "How To Use This Forum"

This is what happens when you do not

If you google , there are explanations of all instructions and the syntax required*
There are also examples in the IDE that will help you with general coding .
I’d recommend Simon Monks “ Getting started book” too.

*Google “ Arduino if”
For example

I still have the same problem :frowning:

MadZArduino:
I still have the same problem :frowning:

So, show us the code.

void loop()

if (digitalRead(sensor4) >= 4) {

{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
}

else
{
digitalWrite(led2, HIGH);
digitalWrite(led1, LOW);

}

}

I CHANGED SOME :slight_smile:

if (digitalRead(sensor4) >= 4) {

{

Oops.

Please remember to use code tags when posting code

void loop()

if (digitalRead(sensor4) >= 4) {

Oops. See reply #4.

Autoformat.

It is your friend.

CTRL-F

This makes it much easier for us to read.
It also is very helpful for finding problems with curly brackets. Hint, several replies have pointed out that you are missing curly brackets for your loop.