S
S
sulim0003332019-01-07 17:45:08
Programming
sulim000333, 2019-01-07 17:45:08

cycles. While, do while, for, what is the difference?

Please explain with understandable definitions. Explain to me like a 3 year old child XD))

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Mercury13, 2019-01-07
@sulim000333

While - first check, then do, and so on in a circle.
Do - first do, then check, and so on in a circle.
They differ only in the case when the condition is not met: while will not pass, but do will pass once.

// Пример 1.
// верно
while (впереди свободно) шаг;
// неверно — можно врезаться в препятствие, если нельзя сделать ни шагу
do шаг while (впереди свободно);

// Пример 2.
// Неверно — пока у вас в руках нет рубашки, условие цикла некорректно
while (рубашка грязная) возьми рубашку;
// верно
do возьми рубашку; while (рубашка грязная);

The for, foreach and other loops are a kind of while (not do!!) loop, made for a special scenario: to go through a certain set of objects.

M
Maxim Timofeev, 2019-01-07
@webinar

The answer is too obvious, if you look at the examples: www.php.su/learnphp/cs/?cycles
Let's say you have a box in which bottles of "beer" and bottles of "milk" are stored.
If your dad asked to wipe all the bottles from the box - this is foreach
If every bottle until the box runs out of beer - this is while
If every bottle until there are 4 wiped bottles of beer - this is also while
If every bottle until the box runs out of beer, but since I took the last bottle with milk, then be kind, wipe it - this is do-while
If from the 3rd to the 12th - this is for
If only with milk - this is mom, not dad

@
@pestunov, 2019-02-24
_

In general, these three cycles are interchangeable, i.e. if there is a program written using one of them, then it can always be rewritten using any other. But for good style and for convenience, the recommendations are as follows:
for is used when the number of iterations is known (there is a variable or constant that determines this number).
while and do-while are used when the number of iterations is not known in advance.
while is used when there is a possibility that the loop will never execute, while do-while should be used when it is known that at least one iteration is always required.
For example:
for - display N numbers on the screen (N defines the number of iterations).
while - Euclid's algorithm for calculating GCD (if one of the numbers is 0, then there will be no iterations)
do-while - implementation of the game "Guess the number". (at least one attempt is always required, the exact number is unknown).

Z
Zanak, 2019-01-23
@Zanak

Somehow, I read the answers of colleagues, and was disappointed. It is clear that the question is not simple, but very simple, but it is possible to be more accurate in the presentation. In general, my 5 cents.
Any loop is a piece of code, which is usually called the body of the loop, and which we want to execute several times. The number of repetitions can be set in advance, for example: loop from 0 to 10, depend on the data, for example: for each element of the array, or depend on the value that we calculate in the body of the loop, for example: while f (x) > 0, execute. At least in all programming languages ​​known to me, there is a loop exit operator.
while. Loop with precondition. Runs as long as the condition is true. The check occurs before the execution of the loop body. If the condition is initially false, then the body will never be executed. If the condition never becomes false, then we get an infinite loop. Common mistakes for beginners when using this loop:
- the condition does not depend on the variables that are changed in the body of the loop, and it always turns out to be true
- the condition will never become false, due to its properties, for example: X * X >= 0 is always true
PHP example:

$i = 1;
while ($i <= 10) {
    echo $i;
    $i++;
}

do ... while. Loop with postcondition. The difference from the previous one is that the loop body is executed at least once, and after that the loop exit condition is checked. To the typical errors of the previous type of loops, the fact that this loop is guaranteed to be executed once is added, which is not always desirable.
In this example, the loop will run once, even though the condition is false:
$i = 0;
do {
    echo $i;
} while ($i > 0);

for. Most often, this statement describes a loop with a fixed number of repetitions. This is how it might look, for example, for php:
for ($i = 1; $i <= 10; $i++) {
    echo $i;
}

but there are languages ​​where this same operator is used to describe a loop that iterates over a set of values, such as the elements of an array. Possible pitfalls of this type of loops include damage to the value of the iteration counter. Changing it, for example, to exit the loop early, is considered bad programming style, and in especially severe cases can lead to hard-to-diagnose errors.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question