Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Do-While | Loops
Introduction to PHP
course content

Зміст курсу

Introduction to PHP

Introduction to PHP

1. First Acquaintance
2. Variables and Data Types
3. Conditional Statements
4. Arrays
5. Loops

bookDo-While

The do-while and while loops are two types of loops in PHP used to repeat a block of code. However, there is a crucial difference between them: The while loop checks the condition before executing the code block, while the do-while loop checks the condition after completing the code block.

It means that the while loop may not execute at all if the condition does not evaluate to true on the first check. On the other hand, the do-while loop will consistently execute at least once, even if the condition does not evaluate to true on the first check.

Syntax


We write commands that should work in a loop in the do block of code. This is the condition that determines whether to continue iterating the loop.

Let's create a loop with a condition that will never be true:

php

main

copy
12345678
<?php $counter = 5; // Declare and initialize the variable do { echo 'Do-while loop'; $counter = $counter + 1; } while ($counter == 5); // Change the condition to 5 ?>

As you can see, the do-while loop executed the code block at least once, even if the condition counter != 0 was not evaluated to true on the first check.

Here is another way to understand the difference between the do-while and while loops:

  • The while loop is like checking before eating. You can only eat once you check if you are hungry;
  • The do-while loop is like eating before checking. You can eat, even if you are not hungry, and then check if you are still hungry.

Which type of loop to choose depends on your specific needs. Use a do-while loop if you want the code block to execute at least once. If you do not wish for the code block to run if the condition does not evaluate to true on the first check, use a while loop.

Завдання

Fill in the blanks in the code to create a do-while loop that prints even numbers from 2 to 10.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 3
toggle bottom row

bookDo-While

The do-while and while loops are two types of loops in PHP used to repeat a block of code. However, there is a crucial difference between them: The while loop checks the condition before executing the code block, while the do-while loop checks the condition after completing the code block.

It means that the while loop may not execute at all if the condition does not evaluate to true on the first check. On the other hand, the do-while loop will consistently execute at least once, even if the condition does not evaluate to true on the first check.

Syntax


We write commands that should work in a loop in the do block of code. This is the condition that determines whether to continue iterating the loop.

Let's create a loop with a condition that will never be true:

php

main

copy
12345678
<?php $counter = 5; // Declare and initialize the variable do { echo 'Do-while loop'; $counter = $counter + 1; } while ($counter == 5); // Change the condition to 5 ?>

As you can see, the do-while loop executed the code block at least once, even if the condition counter != 0 was not evaluated to true on the first check.

Here is another way to understand the difference between the do-while and while loops:

  • The while loop is like checking before eating. You can only eat once you check if you are hungry;
  • The do-while loop is like eating before checking. You can eat, even if you are not hungry, and then check if you are still hungry.

Which type of loop to choose depends on your specific needs. Use a do-while loop if you want the code block to execute at least once. If you do not wish for the code block to run if the condition does not evaluate to true on the first check, use a while loop.

Завдання

Fill in the blanks in the code to create a do-while loop that prints even numbers from 2 to 10.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 5. Розділ 3
toggle bottom row

bookDo-While

The do-while and while loops are two types of loops in PHP used to repeat a block of code. However, there is a crucial difference between them: The while loop checks the condition before executing the code block, while the do-while loop checks the condition after completing the code block.

It means that the while loop may not execute at all if the condition does not evaluate to true on the first check. On the other hand, the do-while loop will consistently execute at least once, even if the condition does not evaluate to true on the first check.

Syntax


We write commands that should work in a loop in the do block of code. This is the condition that determines whether to continue iterating the loop.

Let's create a loop with a condition that will never be true:

php

main

copy
12345678
<?php $counter = 5; // Declare and initialize the variable do { echo 'Do-while loop'; $counter = $counter + 1; } while ($counter == 5); // Change the condition to 5 ?>

As you can see, the do-while loop executed the code block at least once, even if the condition counter != 0 was not evaluated to true on the first check.

Here is another way to understand the difference between the do-while and while loops:

  • The while loop is like checking before eating. You can only eat once you check if you are hungry;
  • The do-while loop is like eating before checking. You can eat, even if you are not hungry, and then check if you are still hungry.

Which type of loop to choose depends on your specific needs. Use a do-while loop if you want the code block to execute at least once. If you do not wish for the code block to run if the condition does not evaluate to true on the first check, use a while loop.

Завдання

Fill in the blanks in the code to create a do-while loop that prints even numbers from 2 to 10.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

The do-while and while loops are two types of loops in PHP used to repeat a block of code. However, there is a crucial difference between them: The while loop checks the condition before executing the code block, while the do-while loop checks the condition after completing the code block.

It means that the while loop may not execute at all if the condition does not evaluate to true on the first check. On the other hand, the do-while loop will consistently execute at least once, even if the condition does not evaluate to true on the first check.

Syntax


We write commands that should work in a loop in the do block of code. This is the condition that determines whether to continue iterating the loop.

Let's create a loop with a condition that will never be true:

php

main

copy
12345678
<?php $counter = 5; // Declare and initialize the variable do { echo 'Do-while loop'; $counter = $counter + 1; } while ($counter == 5); // Change the condition to 5 ?>

As you can see, the do-while loop executed the code block at least once, even if the condition counter != 0 was not evaluated to true on the first check.

Here is another way to understand the difference between the do-while and while loops:

  • The while loop is like checking before eating. You can only eat once you check if you are hungry;
  • The do-while loop is like eating before checking. You can eat, even if you are not hungry, and then check if you are still hungry.

Which type of loop to choose depends on your specific needs. Use a do-while loop if you want the code block to execute at least once. If you do not wish for the code block to run if the condition does not evaluate to true on the first check, use a while loop.

Завдання

Fill in the blanks in the code to create a do-while loop that prints even numbers from 2 to 10.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 5. Розділ 3
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt