All lessons

Your First Program

5 min · Swift

Write and run your very first Swift program, right here on the page.

Swift · Your First Program0/5 cleared

Enemy 1 of 5

Swift is unusual among the languages you'll see in this track: it doesn't require a main function or any wrapping boilerplate at all. You can write statements directly at the top level of a file, and Swift runs them in order from top to bottom. This is sometimes called "script mode," and it's one reason Swift is often praised as an approachable first (or fifth) language — there's less ceremony standing between you and running code.

print is how you display a message. The text you want shown goes inside parentheses and double quotes: print("Hello, World!"). Whatever is between the quotes is printed exactly as written, including spaces and punctuation.

You'll also notice there's no semicolon at the end of the line. Like Kotlin and Python, Swift treats a line break as the natural end of a statement, so semicolons are optional — you almost never see them in idiomatic Swift code, unlike in Rust, Java, or C where they're required on every statement.

You
Binary Treant

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: Hello, World!