PHP do-while Loop
In this page:
do {
// body runs at least once
} while (condition);
Basic do-while Loop
एक do-while loop एक post-test loop है: यह पहले loop body चलाता है, और सिर्फ पहला run पूरा हो जाने *के बाद* condition check करता है — एक regular while loop से उल्टा order, जो body में entered होने से पहले test करता है।
उदाहरण: Basic do-while Loop
<?php
// Declare `$i`, set to `1`
$i = 1;
// Run this block once, then repeat while the trailing `while` condition holds
do {
// Print `$i . "\n"` to the output
echo $i . "\n";
$i++;
} while ($i <= 3);
?>
Login to try C/C++/Java/PHP code in the editor
एक Run Guarantee करना
क्योंकि condition check body चलने के बाद होता है, एक do-while loop कम से कम एक बार execute होने की guarantee है, भले ही condition तुरंत false evaluate होती — यह plain while loop से एकमात्र meaningful behavioral difference है।
उदाहरण: Guaranteeing One Run
<?php
// Declare `$i`, set to `10`
$i = 10;
// Run this block once, then repeat while the trailing `while` condition holds
do {
// Print "This runs at least once, i=$i\n" to the output
echo "This runs at least once, i=$i\n";
} while ($i < 5);
?>
Login to try C/C++/Java/PHP code in the editor
Loop के अंदर Variables Update करना
जिस variable को आपकी condition check करती है उसे loop body के अंदर कहीं बदलना ही होगा, नहीं तो condition हमेशा के लिए exact same result re-evaluate होती रहेगी — एक infinite loop जो कभी terminate नहीं होता, क्योंकि loop के अंदर कुछ भी इसे stopping point की ओर move नहीं कर रहा।
उदाहरण: Updating Variables inside Loop
<?php
$i = 0;
do {
echo $i . "\n";
$i++; // must update, or this loop never ends
} while ($i < 3);
?>
Login to try C/C++/Java/PHP code in the editor
Menu Runs का Simulation
एक menu जिसे कम से कम एक बार अपने options display करने चाहिए — user के अभी कोई choice करने से पहले भी — do-while के लिए एक natural fit है: आप चाहते हैं कि 'show options' step चाहे कुछ भी हो पहले चले, फिर सिर्फ तभी दोहराते रहें जब user एक और round माँगे।
उदाहरण: Simulation of Menu Runs
<?php
// Declare `$choice`, set to "exit"
$choice = "exit";
// Run this block once, then repeat while the trailing `while` condition holds
do {
// Print "1. Start\n2. Settings\n3. Exit\n" to the output
echo "1. Start\n2. Settings\n3. Exit\n";
} while ($choice !== "exit");
?>
Login to try C/C++/Java/PHP code in the editor
Nested do-while Loops
एक do-while loop को दूसरे के अंदर nest करना एक inner loop को outer loop के हर single pass के लिए अपना पूरा guaranteed-at-least-once cycle चलाने देता है, जो grid-like या multi-dimensional repeated structures बनाने के लिए उपयोगी है।
उदाहरण: Nested do-while Loops
<?php
// Declare `$i`, set to `1`
$i = 1;
// Run this block once, then repeat while the trailing `while` condition holds
do {
// Declare `$j`, set to `1`
$j = 1;
// Run this block once, then repeat while the trailing `while` condition holds
do {
// Print "$i,$j " to the output
echo "$i,$j ";
$j++;
} while ($j <= 2);
// Print "\n" to the output
echo "\n";
$i++;
} while ($i <= 2);
?>
Login to try C/C++/Java/PHP code in the editor
do-whileके अंत मेंwhile (condition);के बाद semicolon भूल जाना, जो एक parse error है।- body के अंदर condition variable बदलना भूल जाना, एक infinite loop का कारण बनते हुए।
do-whileइस्तेमाल करना जब body को शुरू से ही condition false होने पर नहीं चलना चाहिए, क्योंकि यह हमेशा एक बार चलता है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: