Edge Conditions – If / else Statements

 

Edge conditions are a key part of all programmatic logics. They involve executing commands to handle what happens when values hit their limits, like a circle being drawn off a screen.

An if / else condition looks like this:

int x = 0;
void setup() {
}
void loop() {
   x = x + 1;
   if ( x > 100) {
      x = 0;
   }
}

In this example, the if command checks to see if x has been incremented past 100. If it has, then the code within the code block is executed.

A code block is essentially a bunch of code that is surrounded by squiggly brackets:

{
  // any code in here is within the code block
  // there might be many many lines of code ... or just one or two. It doesn't matter
}

In the below example, the if command is followed by an else. The code block following the else command gets executed when the if command is not true.

int x = 0;
void setup() {
}
void loop() {

   if ( x > 100) {
      x = 0;
   } else {
      x = x + 1;
   }
}

Can you understand the difference between the example above and the one before it?