While Loops

This program doubles the value of num each time through the loop.

1
2
3
4
5
6
7
8
9
10
11
//Double
#include <iostream>
using namespace std;
void main()
{ int num=1;
  while(num<=100) {
    cout<<num<<"\n";
    num=num*2;
  } //while
  system("pause");
} //main
CODE

To Do:

  • Trace through this to figure out what will be printed.
  • Run this program in your compiler. Modify it to run longer or to print a different sequence.
  • Why is this easier to do with a while loop than a for loop?

NEXT: An endless loop