While Loops

This program has an endless loop. Why? What does it do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
An endless loop is one that starts,
but the boolean expression can never become true.
Don't run this!!!!
Example:

//Endless loop, print 1 forever.
#include <iostream>
using namespace std;
int main()
{ int num=1;
  while (num>0)
    cout<<num<<"\n";
  return 0;
} //main
CODE

To Do:

  • Trace through this to figure out why it is an endless loop.
  • What do you need to do to make the loop end?

NEXT: Ending a total loop with a sentinel value