Your First Swift Program
Prerequisites: Installing Swift
Create a file called hello.swift with this one line:
print("Hello, world!")
Then run it from the folder that contains it:
swift hello.swift
Expected output
Hello, world!
That's a complete Swift program. In a single-file program (a
script), Swift runs the top-level code from top to bottom — no
main function is required.
printwrites its argument to the screen, followed by a new line."Hello, world!"is a string — text in double quotes.- No semicolon is needed at the end of the line.
A slightly longer program:
print("Hello, world!")
print("I am learning Swift.")
print(2 + 3)
Hello, world!
I am learning Swift.
5
Common mistakes
- Using single quotes:
print('Hello')is an error — Swift strings always use double quotes. - Writing
Printwith a capital P. Swift is case-sensitive:cannot find 'Print' in scope.
Practice
Write a program that prints your name on one line and your favorite food on the next.
Show solution
print("Ada")
print("Pizza")