(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, theprint
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.
- Loop statements: This statement allows a block of code to be executed repeatedly. E.g: for-in, while, repeat while, etc.
- 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.
- 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
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
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.