public void myMethod() { while (YOUR-CONDITION) { YOUR-CODE } }
public void infiniteLoop() { while (true) { prn("I loop forever!"); } }
public void neverEntersLoopCodeBlock() { while ("apple".equals("orange")) { prn("Nuts, I never get here!"); } }
public void loop1to4() { //print nums 1 to 4 int num = 1; while (num <= 4) { prn("Number: " + num); num++; } }
public void loop4to1() { //print 4 to 1 int num = 4; while (num >= 1) { prn("Number: " + num); num--; } }
public void loopAndSum1to4() { //sum nums 1 thru 4 int num = 1; int sum = 0; while (num <= 4) { sum = sum + num; num++; } prn("Sum: " + sum); }
public void loopWithVariableIncrement() { //sum ints 10, 20, ..., 100 (increment by 10) int num = 10; int sum = 0; while (num <= 100) { sum = sum + num; num += 10; } prn("Sum: " + sum); }
public playGame() {
while (!hasWinner()) {
playNext();
}
}