← Back to Swift Course | Chapter 3: Control Flow | Lesson 4 of 8

for-in Loop की जानकारी

एक for-in loop आपको एक list के हर item के लिए एक बार code दोहराने देता है, एक roster पर हर नाम एक-एक करके पढ़ने जैसा।
Syntax
swift
for item in collection {
    // use item
}

for i in 1...5 { }      // closed range
for i in 0..<count { }  // half-open range

Iterating Over a Range

एक for-in loop एक numeric range पर iterate कर सकता है, उस range में हर value के लिए body एक बार चलाते हुए।

उदाहरण: Iterating Over a Range

markup
// Loop over `1...5`, binding each item to `i`
for i in 1...5 {
    // Print "Count: \(i)" to the console
    print("Count: \(i)")
}

Iterating Over an Array

वही for-in syntax सीधे एक array पर काम करता है, आपको order में हर element देते हुए।

उदाहरण: Iterating Over an Array

markup
// Declare a constant `fruits`, set to `["apple", "banana", "cherry"]`
let fruits = ["apple", "banana", "cherry"]
// Loop over `fruits`, binding each item to `fruit`
for fruit in fruits {
    // Print "Fruit: \(fruit)" to the console
    print("Fruit: \(fruit)")
}

Half-Open vs Closed Ranges

..< एक half-open range बनाता है जो इसकी upper bound को exclude करता है, जबकि ... एक closed range बनाता है जो इसे include करता है -- array indices पर loop करते समय एक महत्वपूर्ण distinction।

Note: 0..<numbers.count bounds से बाहर गए बिना valid indices पर loop करने का idiomatic तरीका है।

उदाहरण: Half-Open vs Closed Ranges

markup
// Declare a constant `numbers`, set to `[10, 20, 30]`
let numbers = [10, 20, 30]
// Loop over `0..<numbers.count`, binding each item to `index`
for index in 0..<numbers.count {
    // Print "Index \(index): \(numbers[index])" to the console
    print("Index \(index): \(numbers[index])")
}
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. एक C-style for i = 0; i < n; i++ loop उपयोग करना; वह syntax Swift से पूरी तरह हटाया गया -- इसके बजाय for i in 0..<n उपयोग करें।
  2. Closed range ... (दोनों ends include करता है) को half-open range ..< (upper end exclude करता है) के साथ confuse करना।
  3. Loop body के अंदर loop variable modify करने की कोशिश करना इस उम्मीद से कि यह iteration को प्रभावित करे; loop variable हर iteration में एक fresh constant है।
चैप्टर सारांश
  • for item in collection किसी array, range, dictionary, या अन्य sequence के हर element पर iterate करता है।
  • 0..<5 एक half-open range है 5 को exclude करते हुए; 0...5 closed है और 5 include करता है।
  • Loop variable default रूप से एक constant है, हर iteration में फिर से bound।
  • for-in arrays, ranges, strings, dictionaries, और Sequence conform करने वाली किसी भी type पर काम करता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 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.