What is the purpose of ELSE.

Hi, I don't get what an ELSE after an IF statement does.
What's the difference between

int ledPin = 13;

int x = 0;
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } else {
    digitalWrite(ledPin, HIGH);
  }
}

and

int ledPin = 13;

int x = 0;
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } 
  if(x > 5) {
    digitalWrite(ledPin, HIGH);
  }
}

You have a binary test.
In the second example you're testing twice.
That's silly and wasteful.

[/quote]

TheMemberFormerlyKnownAsAWOL:
You have a binary test.
In the second example you're testing twice.
That's silly and wasteful.

Also the second example uses slightly more program storage and has unnecessary complexity.

0x4a53:
Also the second example uses slightly more program storage and has unnecessary complexity.

And it's ugly.

What about dynamically changing data? For example,

if (digitalRead(somepin) == 1)
{...}
else
{...}

is not the same as

if (digitalRead(somepin) == 1)
{...}
if (digitalRead(somepin) != 1)
{...}

because the value can change between tests in the latter case, but not the first.

aarg:
What about dynamically changing data? For example,

if (digitalRead(somepin) == 1)

{...}
else
{...}




is not the same as


if (digitalRead(somepin) == 1)
{...}
if (digitalRead(somepin) != 1)
{...}




because the value can change between tests in the latter case, but not the first.

What does that mean?

What does what mean? If you just don't get it, the state of a digital pin can change in the nanoseconds between the execution of

if (digitalRead(somepin) == 1)
{...}

and

if (digitalRead(somepin) != 1)
{...}

Thus, both could pass (or fail).

Maybe the following example will show you that using ELSE can be useful even if the value cannot change in between tests.

if( (a == 5) || (a == 7) || (a == 9) || (a == 2) || (a == 8) || (a == 10) )
{
...
} 
else 
{
...
}

The if statement can also be used with functions that return a success or fail value. Then you cannot run the function twice.

if( sendMessage(...) )
{
  Serial.print( "Message send" );
}
else
{
  Serial.print( "Message failed" );
}

aarg:
What does what mean? If you just don't get it, the state of a digital pin can change in the nanoseconds between the execution of

if (digitalRead(somepin) == 1)

{...}



and


if (digitalRead(somepin) != 1)
{...}




Thus, both could pass (or fail).

Got it.
Thanks.

Not to mention that else is easier for humans to read and saves you from the embarrassment of getting the tests slightly wrong. E.g. people will write something like

if (x < 5) 
// do something
if x > 5)
//do something else

then wonder why nothing happens when x is equal to 5.

You may think no-one would ever be that silly but I've seen plenty of similar examples.

Steve

Klaus_K:
Maybe the following example will show you that using ELSE can be useful even if the value cannot change in between tests.

if( (a == 5) || (a == 7) || (a == 9) || (a == 2) || (a == 8) || (a == 10) )

{
...
}
else
{
...
}

I don't think I would ever recommend the use of ELSE with a complex IF statement like that because it is impossible to know which part of the IF failed and caused the ELSE to be triggered.

...R

Code inside the condition might affect the condition. Eg:

if(x >= 10) {
  do_something_when_x_reaches_10();
  x = 0; // reset x
}
else {
  do_something_else_when_x_not_10_yet();
}

Robin2:
I don't think I would ever recommend the use of ELSE with a complex IF statement like that because it is impossible to know which part of the IF failed and caused the ELSE to be triggered.

...R

So, you're not a fan of the 'default' case of a switch statement, either?

virtuos1:
Hi, I don't get what an ELSE after an IF statement does.
What's the difference between

int ledPin = 13;

int x = 0;
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } else {
    digitalWrite(ledPin, HIGH);
  }
}



and


int ledPin = 13;

int x = 0;
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  }
  if(x > 5) {
    digitalWrite(ledPin, HIGH);
  }
}

One very important difference is you've had to duplicate the condition, and wherever there is duplication in
code there will be bugs caused by getting the duplication wrong. Here the conditions have to be
complementary too, increasing the scope for mistakes.

And of course without else how would you do "else if":

  if (condition1)
  {  statement1 ; }
  else if (condition2)
  {  statement2; }
  else if (condition3)
  {  statement3; }
  else
  {  statement4; }

without getting really into a tangle?

aarg:
So, you're not a fan of the 'default' case of a switch statement, either?

I'm not sure I'm competent to answer that. I don't think I have ever used it. However, in general, SWITCH/CASE is a very restricted version of IF ELSE so the problem may not arise.

...R

MarkT:
One very important difference is you've had to duplicate the condition, and wherever there is duplication in
code there will be bugs caused by getting the duplication wrong. Here the conditions have to be
complementary too, increasing the scope for mistakes.

And of course without else how would you do "else if":

  if (condition1)

{  statement1 ; }
 else if (condition2)
 {  statement2; }
 else if (condition3)
 {  statement3; }
 else
 {  statement4; }



without getting really into a tangle?
  if (condition1)
  {  statement1 ; }
  if (condition2 && !condition1)
  {  statement2; }
  if (condition3 && !condition2 && !condition1)
  {  statement3; }
  if(!condition3 && !condition2 && !condition1)
  {  statement4; }

I'm going to add a little delay and we'll see how your two examples act differently:

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } else {
    digitalWrite(ledPin, HIGH);
  }
  delay(1000);
}

In this first example the LED is turned off while X is 0, 1, 2, 3, 4, and 5 (about 6 seconds) and turns ON at the 7th iteration.

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } 
  if(x > 5) {
    digitalWrite(ledPin, HIGH);
  }
  delay(1000);
}

In this second example the LED is turned off while X is 0, 1, 2, 3, 4 (5 seconds) but in the 6th iteration when X==5 the value of X is incremented to 6 which triggers the second 'if' and turns the LED on. This causes the LED to come on about a second earlier.

You could fix that particular problem by swapping the two statements:

void loop() {
  if(x > 5) {
    digitalWrite(ledPin, HIGH);
  }

  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } 

  delay(1000);
}

But then if you wanted to repeat the sequence (off 6 seconds, on 1 second) by setting x back to 0, the simple change would have a problem similar to the original:

void loop() {
  if(x > 5) {
    digitalWrite(ledPin, HIGH);
    x = 0;
  }

  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } 

  delay(1000);
}

The LED would be on for only a few microseconds until the second 'if' triggered and turned it off.

You can't just swap the statements this time because that would bring back the original problem and you'd have 5 seconds off and 1 second on rather than 6 and 1.

The way to fix it is to use the 'else':

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
    x++;
  } else {
    digitalWrite(ledPin, HIGH);
    x = 0;
  }
  delay(1000);
}

So an else can't change to true until the next time the loop code runs, while a if can?
And as to your answer you can use two ifs and just put the x++ after the second if.

virtuos1:
So an else can't change to true until the next time the loop code runs, while a if can?

An if/else will only execute one or the other, not both. Two separate 'if' statements are independant and will either execute or not depending on the current value of their separate conditions.

virtuos1:
And as to your answer you can use two ifs and just put the x++ after the second if.

Then you would get a different problem if you want to repeat the sequence:

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
  }

  if(x > 5) {
    digitalWrite(ledPin, HIGH);
    x = 0;
  }

  x++;

  delay(1000);
}

The first cycle would be 6 seconds off and 1 second on, as expected. The x++ is after the x=0 so the second and later cycles would be 5 seconds off and 1 second on.

I guess you could fix that by adding a third 'if':

void loop() {
  if(x <= 5){
    digitalWrite(ledPin, LOW);
  }

  if(x > 5) {
    digitalWrite(ledPin, HIGH);
  }

  x++;

  if(x > 5) {
    x = 0;
  }

  delay(1000);
}

If you REALLY don't want to use 'else' but want the same effect you could emulate it by storing the result of the conditional expression:

void loop() {
  boolean condition = x <= 5;
  if(condition)
  {
    digitalWrite(ledPin, LOW);
    x++;
  }

  if (!condition)
  {
    digitalWrite(ledPin, HIGH);
    x = 0;
  }
  delay(1000);
}