Unit A - Foundations

05. Operators & Decisions

Calculate, compare and select actions.

Estimated time 3 hours

Learning outcomes

  • Use arithmetic, comparison and Boolean operators
  • Distinguish assignment from equality
  • Write if, else if and else chains
  • Combine conditions with AND, OR and NOT

Parts and preparation

Arduino Uno, two push buttons and onboard LED.

Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.

Operators & Decisions instructional connection diagram
Open the diagram for a larger view. Trace every connection before building.

Arithmetic

+, -, , / and % calculate values. Integer division discards the remainder. Use a decimal operand such as 5.0 when a floating-point result is required.

Assignment and comparison

= stores a value. == compares equality. Other comparisons are !=, <, <=, > and >=.

Boolean logic

A && B requires both conditions. A || B requires either condition. !A reverses a condition. Parentheses make complex conditions easier to read.

Decision chains

Only the first true branch of an if / else if / else chain runs. Separate if statements are each tested independently.

Wiring and safe build sequence

  1. Button 1: D2 to GND
  2. Button 2: D3 to GND
  3. Use INPUT_PULLUP for both
  4. Use onboard LED as output
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.
const byte buttonA = 2;
const byte buttonB = 3;

void setup() {
  pinMode(buttonA, INPUT_PULLUP);
  pinMode(buttonB, INPUT_PULLUP);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  bool aPressed = digitalRead(buttonA) == LOW;
  bool bPressed = digitalRead(buttonB) == LOW;

  if (aPressed && bPressed) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else if (aPressed || bPressed) {
    digitalWrite(LED_BUILTIN, millis() / 250 % 2);
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

How the code works

  1. Pressed is LOW because INPUT_PULLUP holds an open input HIGH.
  2. Both buttons give steady light; either button gives flashing light.

Test and record evidence

Expected result: No button: off. One button: flashing. Both buttons: steady on.

Practical evidence checklist

Common faults and checks
  • If behavior is inverted, remember that pressed reads LOW.
  • Check && versus || and == versus =.
Extension challenge: Add a condition that gives button A priority over button B.

Check your understanding

Q1. What is the difference between = and ==?

Show answer

= assigns; == compares.

Q2. When does A || B evaluate true?

Show answer

When A, B or both are true.