← Back to Kotlin Course | Chapter 3: Control Flow | Lesson 4 of 7

while और do-while लूप

while और do-while loops किसी action को तब तक दोहराते रहते हैं जब तक एक condition true रहे, जैसे किसी के जवाब देने तक दरवाज़ा खटखटाना।
Syntax
kotlin
while (condition) {
    // body
}

do {
    // body runs at least once
} while (condition)

The while Loop

while तब तक बार-बार अपना body चलाता है जब तक condition true रहे, हर iteration से पहले -- पहली भी शामिल -- condition जांचते हुए।

उदाहरण: The while Loop

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a mutable variable `count`, initialized to `0`
    var count = 0
    // Keep looping while `count < 5` holds
    while (count < 5) {
        // Print "Count is $count" to the console, with a trailing newline
        println("Count is $count")
        count++
    }
}

The do-while Loop

do-while पहले अपना body चलाता है और बाद में condition जांचता है, यह गारंटी देते हुए कि body कम से कम एक बार execute होगा भले ही condition शुरू से false हो।

उदाहरण: The do-while Loop

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a mutable variable `attempts`, initialized to `0`
    var attempts = 0
    do {
        // Print "Attempt number ${attempts + 1}" to the console, with a trailing newline
        println("Attempt number ${attempts + 1}")
        attempts++
    // Keep looping while `attempts < 3` holds
    } while (attempts < 3)
}

Infinite Loops with break

body के अंदर एक break statement के साथ while (true) combine करना एक ऐसा loop बनाता है जिसकी exit condition शुरुआत में कहने के बजाय logic के बीच में express करना आसान है।

Note: हमेशा सुनिश्चित करें कि while (true) loop में एक reachable break हो, नहीं तो यह हमेशा के लिए चलेगा।

उदाहरण: Infinite Loops with break

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a mutable variable `n`, initialized to `1`
    var n = 1
    // Keep looping while `true` holds
    while (true) {
        // Print "n is $n" to the console, with a trailing newline
        println("n is $n")
        n *= 2
        if (n > 20) break
    }
}

Choosing Between while and do-while

while का उपयोग तब करें जब body को genuinely शून्य बार चलने की ज़रूरत हो सकती है, और do-while तब जब body को जारी रखना है या नहीं यह जांचने से पहले हमेशा कम से कम एक बार चलना चाहिए।

उदाहरण: Choosing Between while and do-while

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `condition`, set to `false`
    val condition = false
    // Keep looping while `condition` holds
    while (condition) {
        // Print "This never prints" to the console, with a trailing newline
        println("This never prints")
    }
    do {
        // Print "This always prints once" to the console, with a trailing newline
        println("This always prints once")
    // Keep looping while `condition` holds
    } while (condition)
}
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. body के अंदर loop की condition variable को update करना भूल जाना, जिससे एक infinite loop बनता है जो कभी खत्म नहीं होता।
  2. while उपयोग करना जब असल में do-while चाहिए, जिससे एक ऐसा block पूरी तरह skip हो जाता है जिसे कम से कम एक बार चलना चाहिए था जब condition शुरू से false हो।
  3. check order को मिला देना: while body चलाने से पहले जांचता है, do-while बाद में जांचता है, जो यह बदल देता है कि body कितनी बार चल सकता है।
चैप्टर सारांश
  • while हर iteration से पहले अपनी condition जांचता है, इसलिए body शून्य बार चल सकता है।
  • do-while हर iteration के बाद अपनी condition जांचता है, body के कम से कम एक बार चलने की गारंटी देते हुए।
  • दोनों loops को condition के आखिरकार false होने की ज़रूरत है, आमतौर पर body के अंदर एक variable update करके।
  • while (true) { ... break ... } उन loops के लिए एक आम pattern है जिनकी exit condition body के अंदर express करना आसान है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

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.