Home » First Swift Program

First Swift Program

by Online Tutorials Library

First Swift Program

To run your first Swift program, you must have installed Xcode and Swift on your computer.

Open the Xcode playground and write down the following code:

Program:

Output:

"Hello World"  

Program Explanation

      1. // This is the first Swift program also called Hello, World! Program
The above line which is started with two slashes (//) is a comment. It is totally ignored by the compiler. Comments are used to make your code understandable by other person.

      2. import Swift
The import Swift keyword is used to access all the symbols defined inside the Swift language. Without using this keyword, you can’t use print statement to print anything on Xcode.

      3. print(“Hello, World!”)
The above line is a print function in Swift. It is used to show the output on the screen.

The “Hello World” program on Terminal

Follow the steps given below:

  1. Open the terminal
  2. Type swift and press enter. You will get a welcome message as Welcome to Apple Swift version x.x.x.
  3. Type print(“Hello, World!”)
  4. Press Enter

Output:

Hello, World!  

Note: Unlike other programming language C, C++, Java, Python etc., Swift doesn’t need to add a semicolon (;) at the end of the statement. It is optional.
You can add multiple lines in a single line by using semicolon (;) at the end of first statement.

Example:


Next TopicSwift Operators

You may also like