June 2025
Intermediate to advanced
1093 pages
33h 24m
English
The while loop executes a statement so long as a specified condition is met. The process is shown in Figure 8.3. You write it like this:
while( Condition ) Statement
For Condition and Statement, everything I mentioned about if applies. Therefore, the following example should pose no difficulty for you.
// https://godbolt.org/z/h466fYq4W#include <iostream>int main() { /* Sum up from 1 to 100 */ int sum = 0; int number = 1; while(number <= 100) // condition { // block that is repeatedly executed sum += number; // for the result number += 1; // next number } // end of the repeated block std::cout << sum << std::endl;}
Listing 8.12 The loop runs 100 times.
Before the loop block is executed, the condition is checked ...
Read now
Unlock full access