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

Ranges क्या हैं

एक range numbers या letters की एक पूरी sequence describe करने का shortcut है, जैसे हर number list करने के बजाय 1-से-10 कहना।
Syntax
kotlin
val range = start..end          // inclusive
for (i in start until end) { }  // excludes end
for (i in start downTo end) { } // descending
for (i in start..end step 2) { }

Inclusive Ranges with ..

.. operator एक range बनाता है जो अपने start और end values दोनों को शामिल करता है, जो आमतौर पर सीधे for loops या in checks के अंदर उपयोग होता है।

उदाहरण: Inclusive Ranges with ..

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `range`, set to `1..5`
    val range = 1..5
    // Loop over `range`, binding each item to `n`
    for (n in range) {
        // Print "Value: $n" to the console, with a trailing newline
        println("Value: $n")
    }
}

Excluding the End with until

until एक range बनाता है जो अपनी upper bound को बाहर रखता है, जो zero से शुरू करके ठीक n बार loop करने का idiomatic तरीका है।

उदाहरण: Excluding the End with until

markup
// Entry point: execution of the program starts here
fun main() {
    // Loop over `0 until 5`, binding each item to `i`
    for (i in 0 until 5) {
        // Print "Index: $i" to the console, with a trailing newline
        println("Index: $i")
    }
}

Counting Down with downTo

downTo एक descending range बनाता है, किसी for loop को एक बड़े number से एक छोटे number तक पीछे की ओर गिनने देते हुए।

उदाहरण: Counting Down with downTo

markup
// Entry point: execution of the program starts here
fun main() {
    // Loop over `5 downTo 1`, binding each item to `i`
    for (i in 5 downTo 1) {
        // Print "Countdown: $i" to the console, with a trailing newline
        println("Countdown: $i")
    }
    // Print "Liftoff!" to the console, with a trailing newline
    println("Liftoff!")
}

Custom Increments with step

किसी range पर .step(n) chain करना बदल देता है कि हर iteration में value कितनी बढ़े (या घटे), आपको values को skip करने देते हुए।

उदाहरण: Custom Increments with step

markup
// Entry point: execution of the program starts here
fun main() {
    // Loop over `0..10 step 2`, binding each item to `i`
    for (i in 0..10 step 2) {
        // Print "Even step: $i" to the console, with a trailing newline
        println("Even step: $i")
    }
}
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. .. उपयोग करना जब upper bound को exclude करना चाहिए, off-by-one errors आम हैं; until (या ..<) end value को exclude करता है।
  2. पीछे की ओर गिनने के लिए downTo भूल जाना; एक साधारण .. range हमेशा बढ़ते order में जाता है।
  3. जब non-1 increment चाहिए तब step function को नज़रअंदाज़ करना, और इसके बजाय loop body के अंदर manually values skip करना।
चैप्टर सारांश
  • a..b a से b तक एक inclusive range बनाता है।
  • a until b (या a..<b) एक ऐसा range बनाता है जो upper bound b को exclude करता है।
  • a downTo b a से b तक एक descending range बनाता है।
  • .step(n) किसी भी range का increment बदल देता है, आपको values skip करने देते हुए।
🔒

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.