Also, the examples and programming assignments will get much more exciting...
I do this because:
The loop statements lets a computer program execute a statement (usually a compound statement) multiple times until a condition is satisfied 
while ( Condition ) ONEsinglestatement // While body 
When you have written enough while statements, I am sure that you will not only memorize this syntax, but can use it effectively.

int x = 1; while ( x < 100 ) System.out.println("x = " + x); 
The first and foremost responsibility is to make sure that the statement inside the while loop will affect the condition so that the while loop will terminate !!!
int x = 1; while ( x < 100 ) { System.out.println("x = " + x); x++; } 

n % f == 0 
Many problems are solved with the computer by trying every possible value
The solutions are identified by testing whether a value works
public class FindDividers { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int f; System.out.print("Enter integer: "); n = in.nextInt(); //  // Try all number from 1 to n to see if it is a divider of n //  f = 1; // start while ( f <= n ) { if ( n % f == 0 ) System.out.println(f); f++; } } } 
Then the while statement is executed...
{ if ( n % f == 0 ) System.out.println(f); f++; }is executed.
This compound statement checks if the remainder of the division n/f is zero ( n % f == 0 )
If so, then f is a divider of n, the number f is printed (otherwise, it is not printed).
So the first time, f = 1, the next time f = 2, and the time after that, f = 3, and so on.
while ( f <= n ) ...is false.
This will happen when f = n + 1.
So, the while statement will be executed for f = 1, f = 2, ..., f = n.
In other words, the test n % f == 0 is tried using f = 1, 2, 3, ..., n and the values that succeeds (remainder is indeed 0) will be printed.


I can tell you from personal experience that unless you write on your own at least 10 "difficult" programs, you will not become a good programmer. (By difficult program I do not mean the ones you have seen so far, these are "toys". BTW, you won't see any "difficult" program in CS170, you need to take CS171, CS424, and other higher level classes)
Example: factor 36
n = 36 try factor 2 36/2 = 18 remainder 0, good > 2 n = 18 try factor 2 18/2 = 9 remainder 0, good > 2 n = 9 try factor 2 9/2 = 4 remainder 1, no good try factor 3 9/3 = 3 remainder 0, good > 3 n = 3 try factor 3 3/3 = 1 remainder 0, good > 3 n = 1 stop ! 
Another example
Example: factor 45
n = 45 try factor 2 45/2 = 22 remainder 1, no good try factor 3 45/3 = 15 remainder 0, good > 3 n = 15 try factor 3 15/3 = 5 remainder 0, good > 3 n = 5 try factor 3 5/3 = 1 remainder 2, no good try factor 4 5/4 = 1 remainder 1, no good try factor 5 5/5 = 1 remainder 0, good > 5 n = 1 stop ! 
If (remainder of division == 0) update: n = n/f (try the same f) else try f + 1
Sketch of algorithm:
input n; f = 2; while ( ??? when should we stop ??? ) { if ( n % f == 0 ) { print factor f; n = n / f ; // f unchanged ! } else { f++; } } 
Now write the algorithm in Java:
public class FindFactors { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; int f; System.out.print("Enter integer: "); n = in.nextInt(); // input n f = 2; // start while ( n != 1 ) { if ( n % f == 0 ) { System.out.println(f); n = n / f; } else { f++; // Try next number } } } } 
We have seen how to design a class in click here
So I will go through the design very quickly.
No inputs needed (you will see why later)
No inputs needed (you will see why later)
public class NumberStuff { /** NumberStuff() * Constructor.... */ public NumberStuff(int n) { x = n; // Remember the number } /** findDividers() * Find all numbers that divides x */ public void findDividers() { int f; int n = x; f = 1; // start while ( f <= n ) { if ( n % f == 0 ) System.out.println(f); f++; } } /** findFactors() * Find all factors of number x */ public void findFactors() { int f; int n = x; f = 2; // start while ( n != 1 ) { if ( n % f == 0 ) { System.out.println(f); n = n / f; } else { f++; // Try next number } } } private int x; } 
public class TestNumberStuff { public static void main(String[] args) { Scanner in = new Scanner(System.in); NumberStuff N; int n; System.out.print("Enter integer: "); n = in.nextInt(); N = new NumberStuff(n); System.out.println("The dividers of " + n + " are:"); N.findDividers(); System.out.println(); System.out.println("The factors of " + n + " are:"); N.findFactors(); } } 
public class NumberStuff { public void findDividers() { ... uses instance variable x } public void findFactors() { ... uses instance variable x } private int x; // Instance variable } 
public class NumberStuff { /** findDividers() * Find all numbers that divides x */ public static void findDividers(int x) { int f; int n = x; f = 1; // start while ( f <= n ) { if ( n % f == 0 ) System.out.println(f); f++; } } /** findFactors() * Find all factors of number x */ public static void findFactors(int x) { int f; int n = x; f = 2; // start while ( n != 1 ) { if ( n % f == 0 ) { System.out.println(f); n = n / f; } else { f++; // Try next number } } } } 
public class TestNumberStuff { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n; System.out.print("Enter integer: "); n = in.nextInt(); System.out.println("The dividers of " + n + " are:"); NumberStuff.findDividers(n); System.out.println(); System.out.println("The factors of " + n + " are:"); NumberStuff.findFactors(n); } } 
int i; i = 7; while ( i < 10 ) { System.out.println( i ); i++; } 
Answer:
7 8 9
int i; i = 7; while ( i < 10 ) System.out.println( i ); i++; 
Answer:
The program is read as: while ( i < 10 ) System.out.println( i ); i++; The program will therefore print: 7 7 .. .. (forever, will NOT STOP because i++ will not be executed !!!)
int i; i = 7; while ( i < 10 ); { System.out.println( i ); i++; } 
Answer:
The program is read as: while ( i < 10 ) ; // Empty statement: does nothing... { // This is a compound statement System.out.println( i ); i++; } So the program will not print anything, nor will the program stop !!!