Operators and Expressions
Learn how to use variables and constants within Swift expressions to add logic to your code.
So far, we have looked at using variables and constants in Swift and also described the different data types. Being able to create variables, however, is only part of the story. The next step is to learn how to use these variables and constants in Swift code. The primary method for working with data is in the form of expressions.
Expression syntax in Swift
The most basic Swift expression consists of an operator, two operands, and an assignment. The following is an example of an expression:
var myresult = 1 + 2
In the above example, the (+
) operator is used to add two operands (1 and 2) together. The assignment operator (=
) subsequently assigns the result of the addition to a variable named myresult
. The operands could just have easily been variables (or a mixture of constants and variables) instead of the actual numerical values used in the example.
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy