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

आपका पहला Swift Program

आपका पहला Swift program code का एक छोटा टुकड़ा है जो computer को screen पर एक friendly message दिखाने को कहता है।
Syntax
swift
print("text")
print("another line")

The Classic Hello World

किसी भी language में traditional पहला program screen पर एक greeting print करता है। Swift में, built-in print function के कारण यह सिर्फ एक line लेता है, एक साधारण script के लिए कोई imports या boilerplate ज़रूरी नहीं।

उदाहरण: The Classic Hello World

markup
print("Hello, World!")

Running the Program

एक Swift file को swift command को पास करके execute किया जाता है, जो एक step में इसे compile और तुरंत चलाता है, किसी भी output को terminal में print करते हुए।

उदाहरण: Running the Program

bash
swift hello.swift

⚠️ Run this in your own terminal or Node.js environment.

Adding More Statements

एक Swift program बस top से bottom तक executed statements का एक sequence है। आप ज़्यादा print calls जोड़ सकते हैं, और हर एक program चलने पर अपनी खुद की output line produce करता है।

उदाहरण: Adding More Statements

markup
// Print "Hello, World!" to the console
print("Hello, World!")
// Print "Welcome to Swift" to the console
print("Welcome to Swift")
// Print "This is my first program" to the console
print("This is my first program")
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. यह भूल जाना कि Swift statements को एक main function या class wrapper की ज़रूरत नहीं -- एक single file में top-level code बस सीधे चलता है।
  2. File extension गलत नाम देना (जैसे .swift के बजाय .txt), जो swift command को इसे पहचानने से रोकता है।
  3. एक command-line Swift script चलाते समय terminal के बजाय एक popup window में output की उम्मीद करना।
चैप्टर सारांश
  • एक minimal Swift program एक single line हो सकता है: print("Hello, World!")।
  • Swift files .swift extension उपयोग करती हैं और swift filename.swift से चलाई जाती हैं या swiftc से compile की जाती हैं।
  • main.swift (या एक single-file script) में top-level code सीधे execute होता है, किसी wrapper class की ज़रूरत नहीं।
  • यह simplicity एक कारण है कि Swift beginners के लिए approachable है।
🔒

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.