English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about Swift expressions, statements, and blocks.
In the previous chapter, although expressions, statements, and blocks were used in each Swift program, their meanings were not explained.
Knowing what variables and operators are, it is easier to understand the following concepts explained in detail.
An expression is a value that produces another value, a constant,variables,operatorsandFunctionscombination. In simpler terms, an expression is any valid code that returns a value.
The result value is usuallySwift Data TypesOne of them, for example, is an integer, string, floating-point number, or more complex data types as functions.
Simple statements in Swift 12 if true && false{ print("This is false") }
In the above program, the following expressions are:
Simple statements in Swift 12 , true && false and "This is false"
The expression letsomeValue: Int=12Specify a value in the variable someValue using the assignment operator =12, and represents the value in memory (12)
The expression true && false uses the logical AND operator && to combine the two boolean values true and false without assigning the result false to a variable/Constants.
Similarly, "This is false" represents a string expression.
Sentences are commands that define the operations that a program should execute. Common actions include declaring variables, assigning values, invoking methods, transferring control flow, traversing collections, and applying conditions.
语句和表达式之间的区别在于,语句不返回结果,而是针对某些操作执行,而表达式始终返回结果。
The difference between statements and expressions is that statements do not return a result but perform certain operations, while expressions always return a result. ++If you are familiar with other programming languages, such as C, CIf you are familiar with other programming languages, such as Java, then a semicolon must be used at the end of a statement.(;)
.
But in Swift, it is optional to write a semicolon at the end of a statement. However, if multiple separate statements appear on the same line, a semicolon (;) must be used.
: Swift statements
print("Hello, World!")
Even this is valid:
print("Hello, World!");
In this case, the meaning of the command 'print' is 'display on the screen'. When writing this code in the Playground, you will output the command to the console output 'Hello, World!'.
. Simple statements
Simple statements in Swift 12
let someValue: Int =12It is a simple expression that assigns a value12of statements. It is a command to assign a value to the constant someValue at the same time.
These types of statements allow the program to modify various aspects of the compiler's behavior. Swift has two compiler control statements as follows:
Conditional compilation block
Conditional compilation blocks allow code to be compiled based on the value of one or more compilation conditions. Each conditional compilation block starts with #if and ends with #endif. A simple conditional compilation block has the following format:
#if compilation condition Statements #endif
Example4: Conditional control statements
#if swift(>=4.0) print(""" Hello, World "" #endif
Conditional Swift(>=4.0) is applicable to statements #if #endif. Therefore, only when the Swift version is greater than or equal to4.0, then execute the print statement.
Line control statements
This type of control statement is intended to be used by tools that automatically generate source code. Therefore, as a beginner, you will never use it.
This statement is used to specify the execution flow in a program. Swift has several types of control flow statements.
Loop statements: This statement allows a segment of code to be executed repeatedly. For example: for-in, while, repeated while, etc.
Branch statements:This statement allows certain code blocks to be executed only when certain conditions are met. For example: if else, guard, switch, etc.
Control Transfer Statements:This statement allows changing the method of code execution. For example: break, continue, fallthrough, throw, return, etc.
A code block is a group of statements (zero or more) enclosed in curly braces {}.
The statements within the code block include declarations, expressions, and other types of statements. They are executed in the order they appear in the source code.
It has the following form:
{ statements }
if true{ //Block starts let sum = 2+3 print("Result is \(sum)") //Block ends }
There are two statements let sum= in the block above:2+3 and print("Result is\(Sum)").