JAVA · ОСНОВЫ · LESSON 11

As long as the condition is

while loop · about 14 minutes

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

Repeat to the border.

while checks the condition before each step. While true answers, the commands inside are repeated.

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

✦ There will be 1, 2, 3.

STEP 2

Change the meter.

Inside the loop, increase step. Otherwise, the condition may always remain true, and the program will not stop.

step++;
loop
Verificationstep <= 3Action.show offNext step.step++

✦ It's like adding 1 to step and saving the answer.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the signs and brackets.

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

Solve it yourself

Start with int count = 1. Using while, output numbers from 1 to 4. Do not forget to change count inside the loop.

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

Opening up your homework