Swift में Semicolons
statement1
statement2
statement1; statement2 // semicolon only to share a line
No Semicolons Needed
Swift statements एक newline से terminate होते हैं, इसलिए आपको हर line के आखिर में एक semicolon जोड़ने की ज़रूरत नहीं, C या Java के विपरीत।
उदाहरण: No Semicolons Needed
// 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)
Login to try C/C++/Java/PHP code in the editor
Semicolons for Same-Line Statements
अगर आप एक single line पर दो statements लिखना चाहें, Swift इन्हें अलग करने के लिए एक semicolon चाहता है, क्योंकि boundary मार्क करने के लिए कोई newline नहीं।
उदाहरण: Semicolons for Same-Line Statements
let x = 1; let y = 2; print(x + y)
Login to try C/C++/Java/PHP code in the editor
Idiomatic Style
हालांकि Swift किसी भी statement पर एक trailing semicolon allow करता है, idiomatic Swift code इसे छोड़ देता है क्योंकि यह कोई value नहीं जोड़ता और code को clutter करता है।
उदाहरण: Idiomatic Style
let language = "Swift"
print(language)
// no semicolons needed above, and none are idiomatic here
Login to try C/C++/Java/PHP code in the editor
- दूसरी languages की आदत से semicolons जोड़ना; हालांकि legal हैं, ये अनावश्यक और non-idiomatic Swift हैं।
- यह भूल जाना कि जब एक ही line पर दो statements लिखें तो एक semicolon ज़रूरी है।
- यह मान लेना कि 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: