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

for लूप

एक for loop computer को किसी group के हर item के लिए एक बार एक action दोहराने को कहता है, जैसे किसी list के हर नाम को ज़ोर से पढ़ना।
Syntax
kotlin
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

markup
// 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")
    }
}

Looping with until

for (i in 0 until n) 0 से लेकर n तक, लेकिन उसे शामिल किए बिना, loop करता है -- zero-based indices का उपयोग करके किसी चीज़ को ठीक n बार दोहराने का idiomatic तरीका।

उदाहरण: Looping with until

markup
// 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")
    }
}

Looping Over a Collection

एक for loop सीधे किसी List, Set, या array पर iterate कर सकता है, manually किसी index को track किए बिना आपको बारी-बारी हर element देते हुए।

उदाहरण: Looping Over a Collection

markup
// 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")
    }
}

Looping Over a Map

किसी Map को iterate करना हर entry को loop header के अंदर सीधे एक key और एक value में destructure करने देता है, एक अलग .key/.value access से बचते हुए।

उदाहरण: Looping Over a Map

markup
// 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")
    }
}
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. एक classic C-style for (i = 0; i < 10; i++) loop उपयोग करने की कोशिश करना; Kotlin में सिर्फ for (x in range) style है।
  2. किसी Map को iterate करते हुए destructuring shortcut भूल जाना, ज़रूरत से ज़्यादा verbose entry-based access लिखते हुए।
  3. किसी mutable list को for loop से सीधे iterate करते हुए उसे modify करना, जो एक ConcurrentModificationException का कारण बन सकता है।
चैप्टर सारांश
  • Kotlin का for loop किसी भी चीज़ पर 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 for loop को support नहीं करता; ranges उस use case की जगह लेते हैं।
🔒

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.