Swift Expressions, Statements and Code blocks

In this article, you will learn about Swift expressions, statements and 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 Expressions

An expression is a combination of values, constants, variables, operators, and functions that produces another value. To be more simpler, an expression is any valid code that returns a value.

The resulting value is usually one of Swift Data Types e.g integer, string, and float or more complex data type as functions.

Example 1: Swift Expressions in a Program

let someValue:Int = 12
if true && false{
    print("This is false")
}

In the above program, here are the expressions:

let someValue:Int = 12 ,
true && false and 
"This is false"

The expression let someValue:Int = 12 uses the assignment operator = to assign value twelve in the variable someValue and denotes the value (12) in memory.

The expression true && false uses the logical and operator && to combine two boolean values true and false without assigning the result false to a variable/constant.

Similarly, "This is false" represents a string expression.


Swift Statements

A statement is a command that defines an action a program should take. Common actions include declaring variables, assigning values, calling methods, transfer the control flow of execution, looping through collections, apply a condition etc.

The difference between statements and expressions is that statements do not return results and are executed for some action, while expressions always return a result.

If you are already familiar with other programming language like C, C ++, Java a semicolon (;) must appear at the end of the statement.

However in Swift, it's optional to write semicolon at the end of the statement. However, you need to include it if separate multiple statements should appear on the same line.

Lets see a basic example of statements that you are already familiar with:

Example 2: Swift statement

print("Hello, World!")

Or even this is valid:

print("Hello, World!");

In this case, the command print means "show on the screen". When you write this code on a Playground, you are giving command to output Hello, World! in the console.


There are three types of Statements in Swift.

1. Simple Statements

These are the most common types of statements in swift that consist of either an expression or a declaration.

Example 3: Simple statement in Swift

let someValue:Int = 12

It is a simple expression that assigns value 12 to constant someValue as well as a statement that commands to assign value 12 in constant someValue.


2. Compiler Control Statements

These type of statements allow the program to change aspects of the compiler’s behavior. Swift has two compiler control statements which are listed below:

  • Conditional compilation block

    A conditional compilation block allows code to be only compiled depending on the value of one or more compilation conditions. Every conditional compilation block begins with the #if and ends with #endif. A simple conditional compilation block has the following form:
    #if compilation condition
    Statements
    #endif

    Example 4: Conditional control statement

    #if swift(>=4.0)
    print("""
        Hello,
        World
        """)
    #endif
    The condition swift(>=4.0) is applied at the statement #if #endif . As a result, the print statement is executed only if swift version is greater than or equal to 4.0 at compile time.
     
  • Line control statement

    This type of control statements is intended to be used by tools that auto-generate source code. Therefore, you would never use it as a beginner. You can learn more about it in: Swift Line Control Statement.

3. Control Flow Statements

This statements are used to control the flow of execution in a program. There are several types of control flow statements in Swift.

  1. Loop statements: This statement allows a block of code to be executed repeatedly. E.g: for-in, while, repeat while, etc.
  2. Branch statements: This statement allows a certain block of code to be executed only when certain conditions are met. E.g: if else, guard, switch, etc.
  3. Control transfer statements: This statement allows a way to alter the order in which code is executed. E.g: break, continue, fallthrough, throw, return, etc.

Swift Code Blocks

A code block is a group of statements (zero or more) that is enclosed in curly braces { }.

The statements inside a code block include declarations, expressions, and other kinds of statements. They are executed in order of their appearance in source code.

It has the following form:

{
	statements
}

Example 5: Swift code block

if true{
//start of block
	let sum = 2+3
	print("Result is \(sum)")
//end of block
}

There are two statements let sum = 2+3 and print("Result is \(sum)") inside the block above.