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.

Challenge

+25 XP

Read a number n and print its multiplication table from 1 to 10. Input: 7 Output: 7 x 1 = 7 7 x 2 = 14 ... 7 x 10 = 70

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.