For Loops

In this example, multiply the value of the variable instead of incrementing or decrementing.

1
2
3
4
5
6
7
8
9
10
//for loop prints powers of 2: 1, 2, 4, 8, 16, 32, 64, 128, 256 
#include <iostream>
using namespace std;
void main() { 
  int p; //used to 'drive' the loop
  for (p = 1; p<500; p*=2) { //we multiply instead of adding
	  cout<<p<<"\n";
  } //for loop
  system("pause");
} //main
CODE
output

Note that if you start with 0 and multiply each time, you will have an endless loop!

NEXT: For loop to print the alphabet