C++ Counting

To count in C++, you can use a for loop. Here is an example:

#include <iostream>
using namespace std;

int main() {
    int count = 0;

    for (int i = 1; i <= 10; i++) {
        count += i;
    }

    cout << "The count is: " << count << endl;

    return 0;
}

Explanation:

  1. We include the <iostream> library to use the cout and endl functions.

  2. We declare an integer variable count and initialize it to 0. This variable will keep track of the total count.

  3. We start a for loop with the initialization expression int i = 1, the condition i <= 10, and the increment expression i++. This loop will iterate from i = 1 to i = 10, inclusive.

  4. Inside the loop, we add the current value of i to the count variable using the += operator. This will increment the count by i on each iteration.

  5. After the loop, we use cout to display the final count value by concatenating it with a string message: "The count is: " << count.

  6. Finally, we return 0 to indicate successful program execution.

When you run this code, it will output: "The count is: 55". This is the sum of the numbers from 1 to 10.