Swift - Compound Assignment Operators

Introduction

Compound assignment operators combine the assignment = operator with another operator:

var myNumber = 2000
myNumber = myNumber + 1200  //myNumber is now 3200

Here, the value of myNumber is incremented by 1200.

This statement could be rewritten using the compound assignment operator += :

myNumber  +=  1200  //myNumber is now 3200

The following shows some additional compound assignment operators in use:

var speed = 80
speed -= 15      //speed is now 65

var size = 2
size *= 3        //size is now 6

var width = 100
width /= 2       //width is now 50

Related Topic