While Loops

This loop prints from 10 down to 0. In the second program we combine the decrement num with -- as part of the cout statement.

1
2
3
4
5
6
7
8
9
10
11
12
// Print 10 9 8 7 6 5 4 3 2 1 0 Blast Off!
#include <iostream>
using namespace std;
int main()
{ int num=10;
  while (num>=0) {
    cout<<num<<" ";
	num--;
  }
  cout<<"Blast Off!\n";
  return 0;
} // main
CODE
1
2
3
4
5
6
7
8
9
10
// Print 10 9 8 7 6 5 4 3 2 1 0 Blast Off!
#include <iostream>
using namespace std;
int main()
{ int num=10;
  while (num>=0)
    cout<<num-- <<" ";
  cout<<"Blast Off!\n";
  return 0;
} // main
CODE

NEXT: Doubling a value