Lesson 10 of 34
The for Loop
8 min 25 XP
A for loop has three parts: a start (int i = 1), a condition (i <= 5) and a step (i++). The loop keeps running while the condition is true.
Example: edit me and press Run
Output Loading Java (first time takes a few seconds)
Press Run to see the result here.Create a variable before the loop and add to it inside the loop. This is how we calculate totals.
Example: edit me and press Run
Output Loading Java (first time takes a few seconds)
Press Run to see the result here.A variable declared in the loop, like i, does not exist outside the loop.