Answer the question
In order to leave comments, you need to log in
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
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 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
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).
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++;
}
$i = 0;
do {
echo $i;
} while ($i > 0);
for ($i = 1; $i <= 10; $i++) {
echo $i;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question