← Back to PHP Course | Chapter 4: Control Flow | Lesson 11 of 14

PHP do-while Loop

एक do-while loop पहले soup taste करने और फिर decide करने जैसा है कि cooking continue करनी है या नहीं। यह हमेशा code को कम से कम एक बार चलाता है, और तभी check करता है कि फिर से round लगाना है या नहीं।
Syntax
php
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
<?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);
?>

एक Run Guarantee करना

क्योंकि condition check body चलने के बाद होता है, एक do-while loop कम से कम एक बार execute होने की guarantee है, भले ही condition तुरंत false evaluate होती — यह plain while loop से एकमात्र meaningful behavioral difference है।

उदाहरण: Guaranteeing One Run

php
<?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);
?>

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
<?php
$i = 0;
do {
    echo $i . "\n";
    $i++; // must update, or this loop never ends
} while ($i < 3);
?>

Menu Runs का Simulation

एक menu जिसे कम से कम एक बार अपने options display करने चाहिए — user के अभी कोई choice करने से पहले भी — do-while के लिए एक natural fit है: आप चाहते हैं कि 'show options' step चाहे कुछ भी हो पहले चले, फिर सिर्फ तभी दोहराते रहें जब user एक और round माँगे।

उदाहरण: Simulation of Menu Runs

php
<?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");
?>

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
<?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);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. do-while के अंत में while (condition); के बाद semicolon भूल जाना, जो एक parse error है।
  2. body के अंदर condition variable बदलना भूल जाना, एक infinite loop का कारण बनते हुए।
  3. do-while इस्तेमाल करना जब body को शुरू से ही condition false होने पर नहीं चलना चाहिए, क्योंकि यह हमेशा एक बार चलता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.