O'Reilly logo

Beginning Swift Programming by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

4 Basic Operators

WHAT YOU WILL LEARN IN THIS CHAPTER:            

  • How to assign values to variables and constants using the assignment operator
  • How to perform arithmetic operations using the arithmetic operators
  • How to increment and decrement variables by one using the increment and decrement operators
  • Using increment and decrement variables using the compound assignment operators
  • How to use the nil coalescing operator when dealing with optional types
  • How to perform comparisons using the comparison operators
  • How to specify ranges using the range operator
  • How to perform logical comparisons using the logical operators

In the previous two chapters you learned about the various data types in Swift and how Swift deals with strings and characters. In this chapter, you will learn about the various operators available in Swift. Operators work with the various data types to enable you to make logical decisions, perform arithmetical calculations, as well as change values. Swift supports the following types of operators:

  • Assignment
  • Arithmetic
  • Comparison
  • Range
  • Logical

ASSIGNMENT OPERATOR

The assignment operator (=) sets a variable or constant to a value. In Swift, you can create a constant by assigning a value to a constant name as shown here:

        let companyName = "Developer Learning Solutions"
        let factor = 5

You can also create a variable using the assignment operator:

        var customerName1 = "Richard"

Besides assigning a value to a variable or constant, you can also assign a variable ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required