|
|
public class While02 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) // While-statement { System.out.println(a); // Print a // NO a++ statement !!! } System.out.println("Done"); System.out.println("Exit: a = " + a); } } |
Output:
1 1 .... (never ending printing of 1's) |
How to run the program:
|
Note:
|
|
Otherwise, you will certainly have an infinite loop in the program.
Example:
public class Error01 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) ; // BOGUS semicolon !! { System.out.println(a); // Print a a++; // Increment a } System.out.println("Done"); System.out.println("Exit: a = " + a); } } |
public class Error01 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) ; // While body contains no statements { System.out.println(a); // Print a a++; // Increment a } System.out.println("Done"); System.out.println("Exit: a = " + a); } } |
Result:
|
How to run the program:
|
Example:
public class Error02 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) System.out.println(a); // Print a a++; // Increment a System.out.println("Done"); System.out.println("Exit: a = " + a); } } |
public class Error02 { public static void main(String[] args) { int a; a = 1; while ( a <= 10 ) System.out.println(a); // Body of while-loop a++; // Outside the while loop !! System.out.println("Done"); System.out.println("Exit: a = " + a); } } |
Result:
|
How to run the program:
|