for लूप
for (i in 1..5) { } // inclusive range
for (i in 0 until n) { } // excludes n
for (item in collection) { }
Looping Over a Range
for (i in 1..5) inclusive range 1 से 5 तक iterate करता है, उस sequence के हर number के लिए loop body एक बार चलाते हुए।
उदाहरण: Looping Over a Range
// Entry point: execution of the program starts here
fun main() {
// Loop over `1..5`, binding each item to `i`
for (i in 1..5) {
// Print "Number: $i" to the console, with a trailing newline
println("Number: $i")
}
}
Login to try C/C++/Java/PHP code in the editor
Looping with until
for (i in 0 until n) 0 से लेकर n तक, लेकिन उसे शामिल किए बिना, loop करता है -- zero-based indices का उपयोग करके किसी चीज़ को ठीक n बार दोहराने का idiomatic तरीका।
उदाहरण: Looping with until
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `n`, set to `4`
val n = 4
// Loop over `0 until n`, binding each item to `i`
for (i in 0 until n) {
// Print "Iteration $i" to the console, with a trailing newline
println("Iteration $i")
}
}
Login to try C/C++/Java/PHP code in the editor
Looping Over a Collection
एक for loop सीधे किसी List, Set, या array पर iterate कर सकता है, manually किसी index को track किए बिना आपको बारी-बारी हर element देते हुए।
उदाहरण: Looping Over a Collection
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `fruits`, set to `listOf("apple", "banana", "cherry")`
val fruits = listOf("apple", "banana", "cherry")
// Loop over `fruits`, binding each item to `fruit`
for (fruit in fruits) {
// Print "Fruit: $fruit" to the console, with a trailing newline
println("Fruit: $fruit")
}
}
Login to try C/C++/Java/PHP code in the editor
Looping Over a Map
किसी Map को iterate करना हर entry को loop header के अंदर सीधे एक key और एक value में destructure करने देता है, एक अलग .key/.value access से बचते हुए।
उदाहरण: Looping Over a Map
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `prices`, set to `mapOf("apple" to 3, "banana" to 1)`
val prices = mapOf("apple" to 3, "banana" to 1)
for ((fruit, price) in prices) {
// Print "$fruit costs $$price" to the console, with a trailing newline
println("$fruit costs $$price")
}
}
Login to try C/C++/Java/PHP code in the editor
- एक classic C-style
for (i = 0; i < 10; i++)loop उपयोग करने की कोशिश करना; Kotlin में सिर्फfor (x in range)style है। - किसी
Mapको iterate करते हुए destructuring shortcut भूल जाना, ज़रूरत से ज़्यादा verbose entry-based access लिखते हुए। - किसी mutable list को
forloop से सीधे iterate करते हुए उसे modify करना, जो एकConcurrentModificationExceptionका कारण बन सकता है।
- Kotlin का
forloop किसी भी चीज़ पर iterate करता है जो एक iterator प्रदान करती है: ranges, collections, arrays, और strings। for (i in 0 until n)बिना off-by-one error के एक निश्चित संख्या में loop करने का idiomatic तरीका है।- किसी
Mapको iterate करना आपको loop header में सीधे हर entry को(key, value)में destructure करने देता है। - Kotlin एक C-style three-part
forloop को support नहीं करता; ranges उस use case की जगह लेते हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: