The condition must evaluate to true/false. public foo() { for (INITIALIZER, CONDITION, INCREMENTOR) { YOUR-CODE } }
public void loop() { //print ints 1 to 10 for (int n = 1; n <= 10; n++) { prn("n: " + n); } }
public void loop() { //print 10 to 1 for (int n = 10; n >= 1; n--) { prn("n: " + n); } }
public void demoSum() { //sum ints 1 thru 10 int sum = 0; for (int n = 1; n <= 10; n++) { sum = sum + n; } prn("Sum: " + sum); }
public void demoSum() { //sum ints 10, 20, ..., 100 (increment by 10) int sum = 0; for (int n = 10; n <= 100; n += 10) { sum = sum + n; } prn("Sum: " + sum); }
public void demoObjectsInLoops() { //Increase width by 2 and height by 1 during each loop Rectangle rec = new Rectangle(); println("Before: " + rec.toString()); for (int n = 1; n <= 10; n++) { rec.setWidth(rec.getWidth() + 2); rec.setHeight(rec.getHeight() + 1); } println("After: " + rec.toString()); }