The "for" loop is a shortcut-form of the while loop when a local incrementor is used.

Here, we'll convert a few of the "while loop" examples to "for loop" form.

General Form

The initializer runs first and then the loop starts and continues while condition is true. After each loop the incrementor executes. Again, the code block can be any code.
The condition must evaluate to true/false.
public foo() {
	for (INITIALIZER, CONDITION, INCREMENTOR) {
		YOUR-CODE
	}
}
Simple Loop And Print

Basically we count up to a number, terminating after we get there.

👉🏼 Try It

📹 Video
public void loop() {
	//print ints 1 to 10
	for (int n = 1; n <= 10; n++) {
		prn("n: " + n);
	}
}
Reverse Loop And Print

We can also count down to a number. Remember to decrement (--) or we'll be in infinite loop again.
public void loop() {
	//print 10 to 1
	for (int n = 10; n >= 1; n--) {
		prn("n: " + n);
	}
}
Sum

It is common to compute a sum over a range.
public void demoSum() {
	//sum ints 1 thru 10
	int sum = 0;
	for (int n = 1; n <= 10; n++) {
		sum = sum + n;
	}
	prn("Sum: " + sum);
}
Variable Incrementor

We can increment by values other than 1. Here we increment by 10.
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);
}
With Proper Objects

We will definitely use objects in our loops.

👉🏼 Try It

📹 Video
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());
}