Цикл While
In the previous chapter, you could see that with the help of a loop, we repeated the code 10 times. Now, let's examine the syntax of one of these loops.
У попередньому розділі ви могли бачити, що за допомогою циклу ми повторили код 10 разів. Тепер давайте розглянемо синтаксис одного з таких циклів.
Main.java
123while (condition) { // code to be executed }
To remember how this loop works, you can follow a simple rule: While the condition is true, perform the operation. For example, while it's raining, I use an umbrella. As soon as the rain stops, I go without an umbrella.
It's raining - the condition
I use an umbrella - the code executed inside the loop
The rain has stopped - the compiler exits the loop and stops executing the code inside the loop.
It's that simple.
Щоб запам'ятати, як працює цей цикл, можна скористатися простим правилом: Поки умова істинна, виконуйте операцію. Наприклад, поки йде дощ, я використовую парасольку. Як тільки дощ припиняється, я йду без парасольки.
Йде дощ - умова
Я беру парасольку - код, що виконується всередині циклу
Дощ припинився - компілятор виходить з циклу і припиняє виконання коду всередині циклу.
Ось так просто.
Here's an example to demonstrate the while
loop:
Main.java
123456789101112131415package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 10; while (a != b) { a = a + 1; System.out.println("a has value: " + a); b = b - 1; System.out.println("b has value: " + b); } System.out.println("Is a equals to b: " + (a == b)); } }
Ось приклад для демонстрації циклу while
:
Swipe to start coding
Print the numbers from 1 to 5 using a while
loop.
Рішення
solution.java
Дякуємо за ваш відгук!
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Awesome!
Completion rate improved to 2.7
Цикл While
Свайпніть щоб показати меню
In the previous chapter, you could see that with the help of a loop, we repeated the code 10 times. Now, let's examine the syntax of one of these loops.
У попередньому розділі ви могли бачити, що за допомогою циклу ми повторили код 10 разів. Тепер давайте розглянемо синтаксис одного з таких циклів.
Main.java
123while (condition) { // code to be executed }
To remember how this loop works, you can follow a simple rule: While the condition is true, perform the operation. For example, while it's raining, I use an umbrella. As soon as the rain stops, I go without an umbrella.
It's raining - the condition
I use an umbrella - the code executed inside the loop
The rain has stopped - the compiler exits the loop and stops executing the code inside the loop.
It's that simple.
Щоб запам'ятати, як працює цей цикл, можна скористатися простим правилом: Поки умова істинна, виконуйте операцію. Наприклад, поки йде дощ, я використовую парасольку. Як тільки дощ припиняється, я йду без парасольки.
Йде дощ - умова
Я беру парасольку - код, що виконується всередині циклу
Дощ припинився - компілятор виходить з циклу і припиняє виконання коду всередині циклу.
Ось так просто.
Here's an example to demonstrate the while
loop:
Main.java
123456789101112131415package com.example; public class Main { public static void main(String[] args) { int a = 0; int b = 10; while (a != b) { a = a + 1; System.out.println("a has value: " + a); b = b - 1; System.out.println("b has value: " + b); } System.out.println("Is a equals to b: " + (a == b)); } }
Ось приклад для демонстрації циклу while
:
Swipe to start coding
Print the numbers from 1 to 5 using a while
loop.
Рішення
solution.java
Дякуємо за ваш відгук!
Awesome!
Completion rate improved to 2.7single