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
<<compilation-unit>>
LoopExecutionEngine
Loop Pipeline
Attributes / Data Members
-loopCounter : uint32_t
Operations / Methods
+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?
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?
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?
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?
Detailed Explanation:
Compilers generate identical branch/test instructions for both
for and while loops.