while और do-while लूप
In this page:
while (condition) {
// body
}
do {
// body runs at least once
} while (condition)
The while Loop
while तब तक बार-बार अपना body चलाता है जब तक condition true रहे, हर iteration से पहले -- पहली भी शामिल -- condition जांचते हुए।
उदाहरण: The while Loop
// 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++
}
}
Login to try C/C++/Java/PHP code in the editor
The do-while Loop
do-while पहले अपना body चलाता है और बाद में condition जांचता है, यह गारंटी देते हुए कि body कम से कम एक बार execute होगा भले ही condition शुरू से false हो।
उदाहरण: The do-while Loop
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Infinite Loops with break
body के अंदर एक break statement के साथ while (true) combine करना एक ऐसा loop बनाता है जिसकी exit condition शुरुआत में कहने के बजाय logic के बीच में express करना आसान है।
while (true) loop में एक reachable break हो, नहीं तो यह हमेशा के लिए चलेगा।उदाहरण: Infinite Loops with break
// 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
}
}
Login to try C/C++/Java/PHP code in the editor
Choosing Between while and do-while
while का उपयोग तब करें जब body को genuinely शून्य बार चलने की ज़रूरत हो सकती है, और do-while तब जब body को जारी रखना है या नहीं यह जांचने से पहले हमेशा कम से कम एक बार चलना चाहिए।
उदाहरण: Choosing Between while and do-while
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- body के अंदर loop की condition variable को update करना भूल जाना, जिससे एक infinite loop बनता है जो कभी खत्म नहीं होता।
whileउपयोग करना जब असल मेंdo-whileचाहिए, जिससे एक ऐसा block पूरी तरह skip हो जाता है जिसे कम से कम एक बार चलना चाहिए था जब condition शुरू से false हो।- check order को मिला देना:
whilebody चलाने से पहले जांचता है,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: