if else structure

I do not know what to do and I got stuck here:

void loop()
{

if (digitalRead(12)==HIGH)
digitalWrite(0,HIGH);
digitalWrite(1, LOW);
else
digitalWrite(0,LOW);
digitalWrite(1,HIGH);

}

error: 'else' without a previous 'if'

Read this.
Jacques

Format of if-else structure is like this:
1. When you have only one statement to execute after if or else --

if (Condition)
  statement
else
  statement

2. When you have more than one statement to execute after if or else --

if (condition)
{
   statemnet1;
   statement2;
   ................
}

else
{
   statemnet1;
   statement2;
   ................
}

Tip: use the "Auto Format" option of the IDE. This will format your code like this and make it obvious that YOUR indentation does not match the result of the compilation

void loop()
{

  if (digitalRead(12) == HIGH)
    digitalWrite(0, HIGH);
  digitalWrite(1, LOW);
  else
    digitalWrite(0, LOW);
  digitalWrite(1, HIGH);

}

Even better, always put { and } round code to be conditionally executed even if it is only one line of code.

Better still put each { and } alone on its own line then use Auto Format

Even better, always put { and } round code to be conditionally executed even if it is only one line of code.

This is what is known as 'literate programming' which we heard in our good old days of programming. Now-a-days, people use:

void setup(){
}

My tutor of that time would certainly call it 'illiterate programming.' Why? There is nothing wrong!

My tutor of that time would certainly call it 'illiterate programming.' Why? There is nothing wrong!

You need to ask yourself - "What would K&R do?"

Functions are a special case (no pun intended), where the opening brace ALWAYS goes on a new line.

You need to ask yourself - "What would K&R do?"

Or, what does the compiler see?

Answer: The source code with all spaces and new lines removed...

So, there could be 0, 1 or 10 new lines before the opening brace and the compiler would generate the proper machine code.

But, with humans, the only good answer to that seems to be: "one and only one because I say so". Braces cannot be seen with any other code on the same line or else you will be abducted by the bears. If you want others to understand your code and help you, you have to have your braces alone on a single line, because my teacher showed me that's the way it has to be done, ans that's the way I teach my students, so that's the way you have to do it, because I am a teacher, so I am always right. Or because I am a senior engineer, so I am always right.

Besides, "K&R" are my buddies and they told me personnally: BRACES HAVE TO BE ALONE ON THEIR LINE... ALWAYS, ALWAYS, ALWAYS.

There you go. I hope that with capital letters you will finally understand.

If you want others to understand your code and help you, you have to have your braces alone on a single line, because my teacher showed me that's the way it has to be done, ans that's the way I teach my students, so that's the way you have to do it, because I am a teacher, so I am always right. Or because I am a senior engineer, so I am always right.

That's the teaching/training methodology that goes among the teacher/trainer and the pupils/recruits, and it continues upto the time when the pupils/fellows acquire some kind of intellectual level that make them again teachers/trainers. This is not an authoritative approach; rather, it provides a platform for the formation of foundation knowledge based on which future autonomy comes in.

it provides a platform for the formation of foundation knowledge based on which future autonomy comes in.

I also believe that's the way it should be. Unfortunately, there are places where such autonomy can only be attained at the price of beeing ridiculed by pupils/fellows or teachers/trainers that think in a different manner.

Many coding 'guidelines' are the result of appeasing ancillary applications that have nothing to do with coding, such as document generators that look for specific characters in column 1 or keywords starting in column 1.