Programming language construct that performs actions according to boolean conditions.
Operators are special symbols or phrases that you use to check, change, or combine values. Swift supports most standard C operators and improves several capabilities to eliminate common coding errors. The assignment operator =
doesn’t return a value, to prevent it from being mistakenly used when the equal to operator ==
is intended.
Swift supports the four standard arithmetic operators for all number types:
+
)-
)*
)/
)Swift supports all standard C comparison operators:
==
)!=
)>
)<
)>=
)<=
)Logical operators modify or combine the Boolean logic values true
and false
. Swift supports the three standard logical operators found in C-based languages:
!a
)a && b
)a || b
)Swift includes two range operators, which are shortcuts for expressing a range of values.
a...b
)a..<b
)Like C, Swift provides compound assignment operators that combine assignment (=
) with another operation. An example is the addition assignment operator (+=
).
The ternary conditional operator is a special operator with three parts, which takes the form question ? answer1 : answer2
. It’s a shortcut for evaluating one of two expressions based on whether question
is true or false.
Control flow statements are used to control the flow of execution in a program.
Swift has two types of conditional statements that include if
and switch
.
A loop is a block of code that’s executed repeatedly, either a specified number of times or until a certain condition is met. Swift has three kinds of loops for-in
, while
and repeat-while
.
Control transfer statements change the order in which your code is executed, by transferring control from one piece of code to another. Swift has five control transfer statements: continue
, break
, fallthrough
, return
, and throw
.
By understanding and using these operators and control flow statements, you can control the logic and execution of your Swift code effectively.