100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Conditionals in Arduino

Learn how if, else if, else, and switch statements let an Arduino sketch make decisions based on sensor readings and pin states.

Control Flow & FunctionsBeginner8 min readJul 10, 2026
Analogies

Why Conditionals Matter in Arduino

Conditionals let a sketch react to the physical world instead of running the same fixed steps forever. When you read a sensor with analogRead() or a button with digitalRead(), you need a way to say 'if the value crosses a threshold, do one thing; otherwise, do another.' Without conditionals, every loop() iteration would blindly repeat, and your project could never turn on a fan when it gets hot or light an LED when a button is pressed.

🏏

Cricket analogy: It is like a batter reading the delivery: if the ball is short and wide, cut it to the boundary; otherwise, defend. The sketch reads a sensor the way Kohli reads the length before choosing a shot.

if, else if, and else

The basic form is if (condition) { ... }, where the condition is any expression that evaluates to true or false. You chain alternatives with else if to test several ranges in order, and a final else catches everything remaining. Arduino evaluates the branches top to bottom and runs only the first one whose condition is true, then skips the rest — so order matters when your ranges overlap.

🏏

Cricket analogy: A captain sets fielders in tiers: else if the batter is a slogger, pack the boundary; else if he blocks, bring in the ring; else keep it standard. Only the first matching plan is used.

cpp
int sensorPin = A0;
int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int value = analogRead(sensorPin); // 0..1023

  if (value > 800) {
    digitalWrite(ledPin, HIGH);   // very bright / high reading
    Serial.println("HIGH");
  } else if (value > 400) {
    digitalWrite(ledPin, HIGH);
    Serial.println("MEDIUM");
  } else {
    digitalWrite(ledPin, LOW);
    Serial.println("LOW");
  }

  delay(200);
}

Use == to compare values and = to assign. Writing if (x = 5) assigns 5 to x and is always true — a classic bug. The compiler may warn, but it will still upload, so double-check every comparison.

Comparison and Logical Operators

Conditions are built from comparison operators (==, !=, <, >, <=, >=) and combined with logical operators: && (AND), || (OR), and ! (NOT). For example, if (temp > 30 && humidity < 40) is true only when both hold. Arduino short-circuits these: with &&, if the left side is false it never evaluates the right, which is handy when the right side is a slow function call or could fail.

🏏

Cricket analogy: A referral needs two things true: the ball must pitch in line AND hit in line for LBW. With &&, if it pitched outside leg the umpire stops there — no need to check impact.

The switch Statement

When you compare one integer variable against many fixed values, a switch statement is cleaner than a long else-if chain. Each case handles one value, and you must add break; at the end of each case, or execution 'falls through' into the next case. A default: label handles any value not listed. switch only works with integer-compatible types like int, char, or byte — not floats or Strings.

🏏

Cricket analogy: A scoreboard operator switches on runs off a ball: case 0 dot, case 4 boundary, case 6 maximum, default a normal run. Each case handles one exact outcome cleanly.

Forgetting break; in a switch case causes fall-through: the code keeps executing into the next case until it hits a break or the end. Sometimes this is intentional, but it is one of the most common bugs, so add break; unless you have a clear reason not to.

  • if / else if / else run the first branch whose condition is true and skip the rest.
  • Use == for comparison; a single = inside a condition is an assignment bug.
  • Combine conditions with && (AND), || (OR), and ! (NOT); Arduino short-circuits && and ||.
  • switch is cleaner than a long else-if chain when comparing one integer to fixed values.
  • Always end each switch case with break; to avoid unintended fall-through.
  • switch works only with integer-like types (int, char, byte), not float or String.
  • Conditionals are what let a sketch respond to sensors and buttons instead of repeating blindly.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ArduinoProgrammingStudyNotes#ConditionalsInArduino#Conditionals#Arduino#Matter#Else#StudyNotes#SkillVeris#ExamPrep