Control Structure

Lesson 19 - Using if Statement

Learn how if conditions control decisions in Arduino code using relational and logical checks.

Progress indicator

Lesson 19 of 28

Learning Objectives

  • Understand what an if statement does.
  • Write correct if syntax with conditions.
  • Use relational and logical operators inside if conditions.
  • Compare if-only and if-else structures.
  • Avoid common mistakes when writing conditional logic.
  • Use nested if blocks in a readable, beginner-safe way.

Concept Explanation

What is if Statement

An if statement lets your program choose what to do based on a condition.

It is the core of decision-making in Arduino code: read a value, check a condition, then run the correct action.

if Syntax

if (condition) {
  // run this block
}

How if Works

If the condition is true, code inside the block runs. If false, it is skipped.

In loop-based programs, this decision can happen thousands of times per second. That is why clean condition logic matters.

Relational Operators in if

Common operators: ==, !=, >, <, >=, <=.

if (temp > 30) { ... }
if (count == 0) { ... }

Logical Operators in if

Combine conditions using && (AND), || (OR), and ! (NOT).

if (isEnabled && sensor > 100) { ... }
if (!buttonPressed) { ... }

if vs if-else

Use if for single-branch checks. Use if-else when both true and false paths need explicit behavior.

If you only care about one case, use if alone. If both outcomes matter,if-else keeps behavior predictable.

Nested if Statements

You can place one if inside another for multi-step decisions, but keep nesting shallow for readability.

For beginners, prefer 1-2 levels of nesting. If logic gets deeper, split checks into helper variables.

When to Use if

  • React to button press/release
  • Turn outputs on/off by conditions
  • Validate ranges and thresholds

Real-Life Example

A water tank controller can use if: if level is low, turn pump ON; else turn pump OFF.

Example Code

This example uses if-else to control LED state from a button input.

const int buttonPin = 0;
const int ledPin = 2;
int buttonState = LOW;

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

What this example teaches

  • How to read a digital input before decision logic
  • How if-else maps two input states into two output actions
  • How INPUT_PULLUP changes button interpretation (LOW often means pressed)

Example Code Explanation

  1. Read button state into buttonState.
  2. Check if pressed condition is true (buttonState == LOW with INPUT_PULLUP).
  3. If true, turn LED ON.
  4. Else, turn LED OFF.
  5. Loop repeats and checks continuously.

Beginner reading tip

Read this pattern as: input -> condition -> action. Most control programs follow this exact flow.

Common Mistakes with if

  • Using assignment = instead of comparison ==.
  • Missing braces in multi-line if blocks.
  • Ignoring active-low logic when using INPUT_PULLUP.
  • Writing complex nested if blocks without clear grouping.

Best Practices for if

  • Write clear condition expressions and names.
  • Prefer early checks and shallow nesting when possible.
  • Use comments for non-obvious condition logic.
  • Use intermediate boolean flags when conditions get long.

Practice Task

  1. Change logic so LED turns ON only when button is released.
  2. Add one more condition: if a second flag is false, always keep LED OFF.
  3. Write one nested if version and one combined logical-operator version.
  4. Explain which version is easier to read and why.

Try it now

Open simulator workspace and practice condition-based control flow.

Run in Simulator