While Loops

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*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.
*/
package numbers;
public class Numbers {
  public static void main(String[] args) {
	  int num=1;
	  while (num>0) {
		  System.out.println(num+" ");
	  }//while
	}//main
}//class
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: