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:
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.
Bu özelliğin yerine
goto
kullanılabilir.
jayreardon at gmail dot com ¶18 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.