Project 3.07 Section 3 ⚡ Embedded Relevance: Core while do-while for loops Pre-Test vs Post-Test Assembly

3.07 Pre-Test (while) vs Post-Test (do-while) Loops & ARM Assembly Generation

Executive Summary: Exploring loop structures: while (pre-test), do-while (post-test), and for loops. We analyze their assembly generation on ARM Cortex-M, demonstrate why do-while loops execute the loop body at least once, and examine infinite loop super-loops in embedded systems.

💻 1. Annotated Source Code

#include <iostream>
using namespace std;

int main() {

	// while
	int count = 0;

	while (count < 10) {
		cout << "Count: " << count << endl;
		count++;
	}

	// do-while
	int counter2 = 100;

	do {
		cout << "Counter2: " << counter2 << endl;
		counter2++;
	} while (counter2 < 10);

	// for loop
	for (int i = 0; i < 10; i++) {
		cout << "i is " << i << endl;
	}

	// sentinel-controlled repetition

	int input;

	cout << "Enter a non-negative integer (or a negative number to quit):";
	cin >> input;

	while (input >= 0) {
		cout << "You entered: " << input << endl;
		cout << "Enter another (or negative to quit): ";
		cin >> input;
	}

	return 0;
}

📐 2. Architecture & UML Class Model

📐 Loop Constructs & Branch-Loop Overhead Model
+ Public - Private # Protected
<<compilation-unit>> LoopExecutionEngine Loop Pipeline
-loopCounter : uint32_t
+executeWhileLoop(limit: uint32_t) : void
+executeDoWhileLoop(limit: uint32_t) : void
+executeForLoop(limit: uint32_t) : void

📚 3. Core C++ Concepts Deep-Dive

1. Pre-Test vs Post-Test Loops

  • Pre-Test (while, for): Evaluates condition BEFORE executing the body. May execute 0 times if condition is initially false.
  • Post-Test (do-while): Executes the body FIRST, then evaluates the condition at the end. Always executes at least once!

⚡ 4. Embedded Systems & Hardware Reality

1. The Embedded 'Super-Loop' Architecture

Bare-metal microcontrollers without an RTOS use an intentional infinite loop (while (true) { ... }) as the master task scheduler, servicing state machines and peripheral interrupts continuously.

💡 5. Production-Ready Embedded Refactoring

Embedded non-blocking super-loop architecture:

💡 Production-Ready Refactor
#include <cstdint>

void serviceSensors() noexcept;
void updateActuators() noexcept;
void feedHardwareWatchdog() noexcept;

void mainSuperLoop() noexcept {
    while (true) { // Master bare-metal super-loop
        serviceSensors();
        updateActuators();
        feedHardwareWatchdog(); // Reset watchdog counter
    }
}

📝 Knowledge Verification Quiz

Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.

Q1. What is the fundamental operational difference between a 'while' loop and a 'do-while' loop?
A A 'while' loop tests its condition before the first iteration (may execute 0 times); a 'do-while' loop tests its condition after each iteration (guaranteed to execute at least once)
B A 'do-while' loop runs in background threads
C A 'while' loop can only count up to 100
D A 'do-while' loop uses double precision
Detailed Explanation: do-while evaluates at the bottom of the loop, ensuring the loop body executes at least one time.
Q2. What is a 'Super-Loop' architecture in bare-metal microcontroller firmware?
A An infinite while(true) loop in main() that continuously polls inputs, processes state machines, and drives outputs without an operating system
B A loop that overclocks the CPU
C A recursive function that never terminates
D A compiler optimization flag
Detailed Explanation: Super-loops form the foundational execution model of bare-metal microcontrollers, executing sequential tasks in an infinite loop.
Q3. Why is 'feeding the Watchdog Timer' essential inside an embedded super-loop?
A If the firmware hangs in a deadlock or infinite loop, failing to reset the watchdog causes hardware to reboot the MCU safely
B To keep the crystal warm
C To recharge the coin cell battery
D To clear Flash memory sectors
Detailed Explanation: The hardware Watchdog Timer resets the microcontroller if software hangs and fails to reload the counter periodically.
Q4. What does a 'for (init; cond; step)' loop compile to in assembly relative to a 'while (cond)' loop?
A Identical assembly code; 'for' and 'while' are syntactic variations of the same underlying loop construct
B A 'for' loop uses twice as much RAM
C A 'while' loop cannot be unrolled
D A 'for' loop disables compiler optimizations
Detailed Explanation: Compilers generate identical branch/test instructions for both for and while loops.