Swift programming for Beginners – Swift Expressions, Statements and Code blocks

(Swift for Beginners)

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 blockA 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 statementThis 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.

 

Swift programming for Beginners – Swift Expressions, Statements and Code blocks

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!