---
# System prepended metadata

title: How to Use Control Sentences in Arduino?
tags: [Arduino]

---

In [Arduino](https://www.ampheo.com/c/development-board-arduino), “control sentences” basically means control statements – the things that tell your code when and how often to run:

* if, else, else if
* switch
* for
* while, do…while
* plus helpers like break, continue, return

Arduino code is just C/C++, so all of this is standard C syntax.

![Arduino-Programming-4-Conditional-Statements](https://hackmd.io/_uploads/S1o68fdzWg.png)

I’ll show you the most useful ones with small Arduino-style examples.

**1. if, else if, else**

Use these to make decisions.

**Example: turn an LED on when a button is pressed**

```
const int LED_PIN = 13;
const int BUTTON_PIN = 2;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // button to GND
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == LOW) {          // button pressed
    digitalWrite(LED_PIN, HIGH);
  } else {                           // button not pressed
    digitalWrite(LED_PIN, LOW);
  }
}
```


**Key points**

* Condition in () must be true/false (0 or non-0).
* Single = is assignment, == is comparison → always use == in if.
* Use {} braces when you have more than one statement (and it’s good habit even for one).

With else if for multiple ranges:

```
int value = analogRead(A0);

if (value < 200) {
  // low
} else if (value < 600) {
  // medium
} else {
  // high
}
```

**2. switch – many options based on one value**

Good for menus, commands, states.

**Example: control LED mode via serial commands**

```
int mode = 0;

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

void loop() {
  if (Serial.available()) {
    char c = Serial.read();
    if      (c == '0') mode = 0;
    else if (c == '1') mode = 1;
    else if (c == '2') mode = 2;
  }

  switch (mode) {
    case 0:
      digitalWrite(13, LOW);   // off
      break;

    case 1:
      digitalWrite(13, HIGH);  // on
      break;

    case 2:
      // blink slowly
      digitalWrite(13, HIGH);
      delay(500);
      digitalWrite(13, LOW);
      delay(500);
      break;

    default:
      // unknown mode – do nothing or safe state
      break;
  }
}
```


**Key points**

* switch (value) { case …: … break; }
* Always put break; unless you want to fall through to the next case.

**3. for loops – repeat a fixed number of times**

**Example: blink LED 10 times**

```
void setup() {
  pinMode(13, OUTPUT);
}

void loop() {
  for (int i = 0; i < 10; i++) {
    digitalWrite(13, HIGH);
    delay(200);
    digitalWrite(13, LOW);
    delay(200);
  }

  while (true) {
    // Stop here forever after blinking 10 times
  }
}
```


**Typical pattern**

```
for (initialization; condition; step) {
  // body
}
```


* i = 0 → start
* i < 10 → loop while this is true
* i++ → run after each loop

**Array example:**

```
int pins[] = {3, 5, 6};
const int count = sizeof(pins) / sizeof(pins[0]);

void setup() {
  for (int i = 0; i < count; i++) {
    pinMode(pins[i], OUTPUT);
  }
}
```

**4. while and do…while – loop until condition changes**
while

Loops as long as the condition is true.

**Example: wait until a button is pressed**

```
void loop() {
  Serial.println("Waiting for button...");
  while (digitalRead(2) == HIGH) {
    // do nothing, just wait
  }
  Serial.println("Button pressed!");
  delay(500);
}
```


Be careful: if the condition never changes, you get an infinite loop.

do…while

Runs at least once, then checks condition.

```
int count = 0;
void loop() {
  do {
    Serial.println(count);
    count++;
  } while (count < 5);

  while (true) { }   // stop
}
```

**5. break, continue, return**

* break – exit the nearest loop or switch.
* continue – skip rest of current iteration, go to next.
* return – exit the current function.

**Example with break and continue:**

```
void loop() {
  for (int i = 0; i < 10; i++) {
    if (i == 3) continue;  // skip 3
    if (i == 8) break;     // stop loop when i == 8
    Serial.println(i);
  }
  while (true) {}
}
```

**6. Where these go in Arduino code**

Remember the Arduino structure:

```
void setup() {
  // runs once
}

void loop() {
  // runs repeatedly
}
```


All your control statements live inside setup() or loop() (or inside functions you call from them). They control the flow of your program:

* if / switch → choose what to do right now
* for / while / do…while → choose how many times to do it