If-Else logic

I’m new to the fine details of programming, and I was wondering about the logic of If-Else statements. I have two questions.

#1: What does a statement do if it does not meet any of the conditions?

#2: What does a statement do after it has completed a condition within a clause?

It will run what is in the “else” statement.

Skip over any other “else if” statements and the “else” statement and resume the code beneath them normally.

Think of “else if” statements as nested “if” statements. The following fragments are the same Example:

if(color == blue){
  motor.spin(forward, 100, percent);
}
else if(color == red){
  motor.spin(reverse, 100, percent);
}
else{
  motor.stop();
}
if(color == blue){
  motor.spin(forward, 100, percent);
}
else {
  if(color == red){
   motor.spin(reverse, 100, percent);
  }
 else{
   motor.stop();
 }
}
9 Likes