Swift Programming

Swift "Hello World" Program

A "Hello, World!" is a computer program that is often used to illustrate the basic syntax of a programming language with a working program. This program outputs Hello, World! on the screen.

If you want to run this program in your computer, make sure that Swift and Xcode is properly installed. For that, check How to run Swift on your computer?

Let's explore how to create Swift "Hello, World!" program.

Swift Variables, Constants and Literals

What is a Variable?

In programming, variables are used to store data in memory which can be used throughout the program. Each variable must be given a unique name called identifier. It is helpful to think of variables as containers that hold information which can be changed later.

Non technically, you can think of a variable as a bag to store some books in it and those books can be replaced with other books later.

Swift Data Types

A data type is the type of data (value) a variable or constant can store in it. For example, in the article Swift Variables and Constants, you created a variable and a constant to store string data in the memory.

This data can be a text/string ("Hello") or a number (12.45) or just bits (0 &1). Defining the data type ensures only the defined type of data is stored.

Let’s look at a scenario:

Swift Comments

A comment is a programmer-readable explanation or annotation in the source code of a program. It is ignored by the compiler when the code is compiled.

Comments are intended for person reading the code to better understand the intent and functionality of the program. It can be helpful while working on a team to clarify the purpose of the code for other team members or while working solely it can act as a reminder to own.

Swift Typealias

A type alias allows you to provide a new name for an existing data type into your program. After a type alias is declared, the aliased name can be used instead of the existing type throughout the program.

Type alias do not create new types. They simply provide a new name to an existing type.

The main purpose of typealias is to make our code more readable, and clearer in context for human understanding.

Swift Expressions, Statements and Code blocks

In the previous chapter, we used expressions, statements and blocks without explaining what it is although it's used in every Swift program.

After you know what variables, operators are it will be easier to understand these concepts which are explained below in detail.

Swift Ranges

A range is an interval of values. A simple example of range is 0,1,2,3,4,5,6,7,8,9 because the numbers are sequential from 0 to 9.

We can create range in swift using two range operators described below:

1. Closed Range Operator (lowerBound...upperBound)

It includes all the values in the interval(lowerbound to upperBound). It is declared using (3 dots)operator.

E.g: 1...3 Defines range containing values 1,2,3

Swift Characters and Strings

What is a character?

A character is a single symbol (letter, number, etc.). Character in swift are of Character type and is declared as:

let someCharacter:Character

How to declare and assign a character in Swift?

You can assign value in Character type same as String using double quotes " " but it should contain only a single character inside the quotes " ".

Swift Basic Input and Output

Swift Basic Output

You can simply use print(_:separator:terminator:) function to send output to standard output (screen). See Swift function article to learn about functions in Swift.

The function print(_:separator:terminator:) accepts three parameters.

Swift for-in Loop

For-in loops are used to run a set of tasks for a certain number of times. These loops iterate over any sequences such as items in an array, range, or characters in a string.

We also use for-in loop to do some repetitive processes for a fixed amount of time.


Why do we need for-in loop?

Imagine someone told you to write a program that outputs Hello, World! in the screen. Your solution will be:

Pages