Loops (iterative statements): for and while loop; break, continue and return

Kotlin का for loop ranges और collections पर iterate करता है (for item in list), while एक condition पर loop करता है, और break, continue और return flow control करते हैं, labels outer loops target करने के लिए।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

हर Item के साथ कुछ करना

FestConnect Mobile के पास इसकी registrations की list है; अब इसे इन्हें PROCESS करना है: हर एक print करना, paid वालों को count करना, एक specific name ढूँढना। मतलब LOOPING।

Kotlin के loops Java से familiar लगेंगे, एक notable change के साथ: कोई C-style for (i = 0; i < n; i++) loop नहीं है। इसकी बजाय, Kotlin का for हमेशा किसी ITERABLE चीज़ पर iterate करता है, एक range या एक collection, जो ज़्यादा clearly पढ़ता है। यह short lesson Kotlin के for और while loops और control keywords break, continue और return cover करता है, OOP unit से पहले language basics complete करते हुए।

Theory

for Ranges और Collections पर Iterate करता है

Kotlin का for loop हमेशा किसी iterable चीज़ पर walk करता है:

  • एक range: for (i in 1..5) { ... } (1 से 5)
  • एक collection: for (event in events) { ... } (हर item directly)
  • indices के साथ: for (i in events.indices) या for ((i, e) in events.withIndex()) (index और item)

कोई for (i = 0; i < n; i++) नहीं है; Kotlin इसकी बजाय ranges इस्तेमाल करता है (for (i in 0 until events.size))। for (event in events) form everyday वाला है: यह आपको हर element directly हाथ में देता है, कोई index bookkeeping नहीं। while और do-while exactly Java की तरह काम करते हैं: एक condition hold होने तक loop।

Practical

एक List पर for, break और continue के साथ

fun main() {
    val events = listOf("Garba", "Coding", "Robo", "Quiz")

    // for over a collection: each item directly
    for (event in events) {
        if (event == "Robo") continue   // skip Robo
        if (event == "Quiz") break      // stop at Quiz
        println(event)                  // Garba, Coding
    }

    // for over a range with index
    for (i in 1..3) {
        println("Pass $i")              // Pass 1, Pass 2, Pass 3
    }

    // while: condition-controlled, as in Java
    var seats = 3
    while (seats > 0) {
        seats--
    }
    println(seats)                     // 0
}

Theory

break, continue, return, और Labels

तीन control keywords loops को steer करते हैं:

  • break: loop पूरी तरह exit कीजिए
  • continue: अगले iteration पर skip कीजिए
  • return: पूरी FUNCTION exit कीजिए (सिर्फ़ loop नहीं)

Listing में, continue Robo skip करता है और break Quiz पर रुकता है, तो सिर्फ़ Garba और Coding print होते हैं।

NESTED loops के लिए, plain break/continue सिर्फ़ INNERMOST loop को affect करते हैं। एक OUTER loop target करने के लिए, एक label इस्तेमाल कीजिए: loop से पहले outer@ लिखिए, फिर break@outer या continue@outer उस labelled loop पर jump करता है। यह वही nested-loop escape है जिससे आप Dart (BCA405-02) और Java (BCA403) में मिले, Kotlin के @ label syntax के साथ।

Quiz

Kotlin के पास looping के लिए क्या NAHIN है, जो Java के पास है?

  1. while loop
  2. C-style for loop: for (i = 0; i < n; i++); Kotlin इसकी बजाय ranges (for i in 0 until n) या collection iteration इस्तेमाल करता है
  3. break keyword
  4. loop करने की ability बिल्कुल
Show the answer

C-style for loop: for (i = 0; i < n; i++); Kotlin इसकी बजाय ranges (for i in 0 until n) या collection iteration इस्तेमाल करता है

Kotlin के पास deliberately कोई C-style for loop नहीं है (for (i = 0; i < n; i++))। इसकी बजाय, इसका for हमेशा किसी iterable चीज़ पर iterate करता है: एक range (for i in 1..5 या for i in 0 until n), एक collection (for item in list), या indices। यह ज़्यादा clearly पढ़ता है और off-by-one और infinite-loop bugs से बचाता है जो manual counters के साथ common हैं। Option A ग़लत है: Kotlin के पास while है (Java से unchanged)। Option C ग़लत है: break exist करता है। Option D absurd है। तो Kotlin में absent एकमात्र Java looping construct manual-counter for है; आप वही चीज़ ranges से express करते हैं, जो ज़्यादा concise और कम error-prone है। for (item in collection) everyday form है।

Think first

C-Style for Loop क्यों Drop किया?

Java का for (i = 0; i < n; i++) familiar और flexible है। Kotlin ने इसे ranges और collection iteration के favour में क्यों हटाया? फिर tap कीजिए।

Show the answer

क्योंकि C-style for loop bugs और boilerplate का एक frequent source है: off-by-one errors (< बनाम <=), increment करना भूलना (infinite loop), या counter logic subtly ग़लत करना, ये सब आप पहले वाली languages में मिल चुके हैं। Kotlin की ranges (for i in 0 until n) और direct collection iteration (for item in list) SAME intent को ज़्यादा clearly और safely express करती हैं: manage करने के लिए कोई manual counter नहीं, fumble करने के लिए कोई bounds नहीं। जब आप बस हर item चाहते हैं, for (item in list) unbeatable है, कोई index बिल्कुल नहीं। जब आपको एक index चाहिए, for (i in list.indices) explicit और construction से correct है। Manual for हटाना Kotlin की philosophy का हिस्सा है: flexible-but-error-prone constructs पर safer, ज़्यादा expressive constructs को prefer करना। आप कुछ नहीं खोते (ranges हर case cover करती हैं) और readability और कम bugs पाते हैं।

Watch out

Loop Traps

C-Style for ढूँढना: इसकी बजाय ranges (for i in 0 until n) या for (item in list) इस्तेमाल कीजिए।

Loops में .. बनाम until: 1..n में n शामिल है; 0 until n n-1 पर रुकता है (0-based indices के लिए सही)।

Nested Loops में break/continue: ये सिर्फ़ inner loop को affect करते हैं; outer वाले के लिए एक label (break@outer) इस्तेमाल कीजिए।

return पूरी Function Exit करता है: सिर्फ़ loop नहीं; एक loop छोड़ने के लिए break इस्तेमाल कीजिए।

Infinite while: sure कीजिए condition eventually false बने (variable को अंदर बदलिए)।

Theory

Kotlin Basics Complete; अब OOP

अब आपके पास Kotlin की fundamentals हैं: variables और null safety, when और ranges वाले conditionals, collections, और loops, real logic लिखने के लिए काफ़ी। Unit 2 FestConnect Mobile की structure Kotlin की OBJECT-ORIENTED features से build करती है: classes, constructors, named parameters, और अपने खुद twist वाली inheritance (classes default से final होती हैं; inherit करने के लिए आपको इन्हें open mark करना पड़ता है)। आपका Java OOP knowledge directly transfer होता है, Kotlin की concise syntax के साथ। Fundamentals से structure तक।

Summary

Key takeaways

  • Kotlin का for हमेशा किसी iterable चीज़ पर iterate करता है: एक range (for i in 1..5), एक collection (for item in list), या indices।
  • कोई C-style for (i=0; i<n; i++) नहीं है; इसकी बजाय ranges इस्तेमाल कीजिए (for i in 0 until n)।
  • while और do-while Java की तरह काम करते हैं (condition-controlled)।
  • break loop exit करता है, continue अगले iteration पर skip करता है, return पूरी function exit करता है।
  • Nested loops के लिए, labels (outer@ ... break@outer) एक specific outer loop target करते हैं।
  • for (item in collection) everyday form है, आपको हर element कोई index के बिना हाथ में देते हुए।
  • Memory hook: Kotlin ranges और collections पर loop करता है, कोई manual counter for नहीं।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Kotlin

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Loops (iterative statements): for and while loop; break, continue and return · Advance Mobile Technology - I (Major-11-02) · Gri-Learn