← Back to Swift Course | Chapter 1: Setup & Basics | Lesson 7 of 7

Swift में Semicolons

Semicolons वे छोटे dots-with-a-tail हैं जिन्हें कुछ languages हर line के आखिर में चाहती हैं, लेकिन Swift आमतौर पर इन्हें skip करने देती है।
Syntax
swift
statement1
statement2

statement1; statement2   // semicolon only to share a line

No Semicolons Needed

Swift statements एक newline से terminate होते हैं, इसलिए आपको हर line के आखिर में एक semicolon जोड़ने की ज़रूरत नहीं, C या Java के विपरीत।

उदाहरण: No Semicolons Needed

markup
// Declare a constant `a`, set to `5`
let a = 5
// Declare a constant `b`, set to `10`
let b = 10
// Print `a + b` to the console
print(a + b)

Semicolons for Same-Line Statements

अगर आप एक single line पर दो statements लिखना चाहें, Swift इन्हें अलग करने के लिए एक semicolon चाहता है, क्योंकि boundary मार्क करने के लिए कोई newline नहीं।

उदाहरण: Semicolons for Same-Line Statements

markup
let x = 1; let y = 2; print(x + y)

Idiomatic Style

हालांकि Swift किसी भी statement पर एक trailing semicolon allow करता है, idiomatic Swift code इसे छोड़ देता है क्योंकि यह कोई value नहीं जोड़ता और code को clutter करता है।

Note: Swift-lint style guides लगातार semicolons को पूरी तरह हटाने की सिफारिश करते हैं।

उदाहरण: Idiomatic Style

markup
let language = "Swift"
print(language)
// no semicolons needed above, and none are idiomatic here
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. दूसरी languages की आदत से semicolons जोड़ना; हालांकि legal हैं, ये अनावश्यक और non-idiomatic Swift हैं।
  2. यह भूल जाना कि जब एक ही line पर दो statements लिखें तो एक semicolon ज़रूरी है।
  3. यह मान लेना कि Swift आश्चर्यजनक तरीकों से stray semicolons वाले code को चुपचाप reformat कर देगा; अपनी line पर अतिरिक्त semicolons सिर्फ अनावश्यक हैं लेकिन statement boundaries पर कोई error नहीं।
चैप्टर सारांश
  • Swift statements को एक trailing semicolon की ज़रूरत नहीं।
  • एक semicolon सिर्फ उसी line पर लिखे दो statements को अलग करने के लिए ज़रूरी है।
  • Idiomatic Swift style semicolons को पूरी तरह छोड़ देता है, प्रति line एक statement।
  • यह C, Java, या JavaScript जैसी languages से अलग है जहां semicolons mandatory हैं।
🔒

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.