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 variable i and initializes it with the value 1.

  • while (i <= 5) is a while 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 of i 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 of i by 1 in each iteration.

  • The } symbol marks the end of the while loop.

  • return 0; ends the main function and indicates that the program has executed successfully.