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

Your First Swift Program

Your first Swift program is a tiny piece of code that tells the computer to display a friendly message on the screen.

The Classic Hello World

The traditional first program in any language prints a greeting to the screen. In Swift, this takes just one line thanks to the built-in print function, with no imports or boilerplate required for a simple script.

Example: The Classic Hello World

markup
print("Hello, World!")

Running the Program

A Swift file is executed by passing it to the swift command, which compiles and immediately runs it in one step, printing any output to the terminal.

Example: Running the Program

bash
swift hello.swift

⚠️ Run this command in your terminal.

Adding More Statements

A Swift program is simply a sequence of statements executed from top to bottom. You can add more print calls, and each one produces its own line of output when the program runs.

Example: Adding More Statements

markup
print("Hello, World!")
print("Welcome to Swift")
print("This is my first program")
Common Mistakes
  1. Forgetting that Swift statements don't need a main function or class wrapper -- top-level code in a single file just runs directly.
  2. Misnaming the file extension (e.g. .txt instead of .swift), which prevents the swift command from recognizing it.
  3. Expecting output in a popup window instead of the terminal when running a command-line Swift script.
Chapter Summary
  • A minimal Swift program can be a single line: print("Hello, World!").
  • Swift files use the .swift extension and are run with swift filename.swift or compiled with swiftc.
  • Top-level code in main.swift (or a single-file script) executes directly, no wrapper class needed.
  • This simplicity is one reason Swift is approachable for beginners.
🔒

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.