AnyCompiler

Your First Swift Program

Unlike Java, C#, or Kotlin, a Swift file doesn't need a class or a main() function at all — code written at the top level of a file runs directly, top to bottom, the moment the program starts. This is sometimes called script-like execution, even though Swift is fully compiled.

Compiling happens first

When you hit run, the compiler checks your entire file and compiles it before a single line executes. If there's a type error or a syntax mistake anywhere, nothing runs at all — you get a compiler error instead of output.

Comments

Comments work like in most C-family languages: anything after // on a line is ignored completely, existing purely so humans reading the code later understand why something is written the way it is.

Run it yourself

Here's a small program with a comment and two print calls. This was actually executed to get the output shown below — try changing the text and running it yourself using the button under the example.

// This is a comment -- ignored by the compiler
print("Hello, world!")
print("Learning Swift one step at a time.")
Output
Hello, world!
Learning Swift one step at a time.

Next: variables and types — let versus var, and Swift's basic types.

Practice exercise

Add a comment above the print statement describing what the program does (the comment itself produces no output), then print exactly: Learning Swift one step at a time.