PHP 8.3.4 Released!

do-while

(PHP 4, PHP 5, PHP 7, PHP 8)

do-while döngüsü, while döngüsüne çok benzer; farkı, doğrulama ifadesinin her yinelemenin başında değil sonunda değerlendiriliyor olmasıdır. while döngüsü ile temek farklılık, do-while kullanıldığında ilk yinelemenin daima yapılıyor olmasıdır (ifadenin doğruluğuna yalnızca yinelemenin sonunda bakılır); halbuki while döngüsünde, ifadenin doğruluğuna her yinelemenin öncesinde bakılmakta ve ifadenin sonucu doğru değilse yineleme hiç başlatılmadan döngü sonlandırılmaktadır.

do-while döngüsü için yalnızca tek bir sözdizimi mevcuttur:

<?php
$i
= 0;
do {
echo
$i;
} while (
$i > 0);
?>

Yukarıdaki döngü tam olarak bir defa çalışacaktır, ilk tekrardan sonra ifadenin doğruluğuna bakıldığında false değerini verecek ($i sıfırdan büyük değildir) ve döngünün çalışması sonlanacaktır.

<?php
do {
if (
$i < 5) {
echo
"i yeterince büyük değil";
break;
}
$i *= $çarpan;
if (
$i < $alt_sınır) {
break;
}
echo
"i uygun";

/* i değerini işle */

} while (0);
?>

Bu özelliğin yerine goto kullanılabilir.

add a note

User Contributed Notes 1 note

up
32
jayreardon at gmail dot com
16 years ago
There is one major difference you should be aware of when using the do--while loop vs. using a simple while loop: And that is when the check condition is made.

In a do--while loop, the test condition evaluation is at the end of the loop. This means that the code inside of the loop will iterate once through before the condition is ever evaluated. This is ideal for tasks that need to execute once before a test is made to continue, such as test that is dependant upon the results of the loop.

Conversely, a plain while loop evaluates the test condition at the begining of the loop before any execution in the loop block is ever made. If for some reason your test condition evaluates to false at the very start of the loop, none of the code inside your loop will be executed.
To Top