While Loop
Programming
This program demonstrates the while loop, which executes a block of code repeatedly as long as a condition is true.
Implementation
#include <iostream>
int main() {
int i = 1;
int max_count = 5;
std::cout << "Max Count: " << max_count << std::endl;
while (i <= 5) {
std::cout << "Count: " << i << std::endl;
++i;
}
return 0;
} Key Concepts
while: Loop that continues while the condition is true++i: Pre-increment operator (increments before use)- The loop variable must be initialized before the loop and updated inside the loop
Output
Max Count: 5
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5