c++ while
#include <iostream>
int main() {
int i = 1;
while (i <= 5) {
std::cout << "Iteration " << i << std::endl;
i++;
}
return 0;
}
The
#include <iostream>
directive is used to include the necessary input/output stream library in the program.The
int main()
function is the starting point of the C++ program.int i = 1;
declares an integer variablei
and initializes it with the value 1.while (i <= 5)
is awhile
loop that continues to execute the code block as long as the condition inside the parentheses is true. In this case, the loop will continue as long as the value ofi
is less than or equal to 5.std::cout << "Iteration " << i << std::endl;
is a statement that prints the current iteration number (i
) to the standard output (console).i++;
is an increment statement that increases the value ofi
by 1 in each iteration.The
}
symbol marks the end of thewhile
loop.return 0;
ends themain
function and indicates that the program has executed successfully.