JAVA · ОСНОВЫ · LESSON 05

Repeat the action.

for loop · about 15 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

The loop takes a few steps.

for repeats the code. At first, step is 1 and then increases by 1 each time. As long as step is no larger than 3, the code inside is repeated.

for (int step = 1; step <= 3; step++) {
    System.out.println(step);
}
Three steps.
Step 1outputStep 2outputStep 3output

✦ On the screen will be 1, 2, 3 — each on a new line.

STEP 2

Three parts in brackets

Before the first ; start. Before the second, check if it is time to stop. step++ takes the next step.

for (int step = 1; step <= 3; step++) {
    System.out.println(step);
}

✦ Remember, the println command itself ends with a semicolon.

IN-CLASS PRACTICE

Type the code yourself

Dial the loop manually. Make sure the output is inside the brackets.

public class Main {
    public static void main(String[] args) {
        for (int count = 1; count <= 3; count++) {
            System.out.println(count);
        }
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Inside main, write the for loop. Let the variable step output numbers from 1 to 4 inclusive, each on a new row.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework