Control Flow
While Loop
A while loop repeats while its condition remains true.
While Loop
while_loop.cpp
#include <iostream>
int main() {
int start = ;
int current = start;
int total = 0;
while (current > 0) {
total += current;
current--;
}
std::cout << "start=" << start << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int start = ;
int current = start;
int total = 0;
while (current > 0) {
total += current;
current--;
}
std::cout << "start=" << start << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
#include <iostream>
int main() {
int start = ;
int current = start;
int total = 0;
while (current > 0) {
total += current;
current--;
}
std::cout << "start=" << start << std::endl;
std::cout << "total=" << total << std::endl;
return 0;
}
while
A `while` loop checks the condition before each pass through the loop body.