C++ For Loop Examples
Real Life Example
To demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:
In this example, we create a program that only print even numbers between 0 and 10 (inclusive):
Here we only print odd numbers:
In this example we print the powers of 2 up to 512:
And in this example, we create a program that prints the multiplication table for a specified number:
Example
int number = 2;
int i;
// Print the multiplication table for the
number 2
for (i = 1; i <= 10; i++) {
cout << number << " x " << i
<< " = " << number * i << "\n";
}
Try it Yourself »