Swift Division Assignment Operator

In Swift, Division Assignment Operator is used to compute the division of given value in left operand by the value in right operand, and assign the quotient in this division operation to the variable given as left operand.

In this tutorial, we will learn about Division Assignment Operator in Swift language, its syntax, and how to use this operator in programs, with examples.

Syntax

The syntax of Division Assignment Operator is

x /= value

where

Operand / SymbolDescription
xLeft operand. A variable.
/=Symbol for Division Assignment Operator.
valueRight operand. A value, an expression that evaluates to a value, a variable that holds a value, or a function call that returns a value.

The expression x += value computes x + value and assigns this computed value to variable x.

Therefore the expression x += value is same as x = x + value.

ADVERTISEMENT

Examples

1. Division Assignment of integer value to x

In the following program, we shall division assign an integer value of 25 to variable x using Division Assignment Operator.

main.swift

var x = 110
x /= 25
print("x is \(x)")

Output

x is 4

Explanation

x /= 25

x = x / 25
  = 110 / 25
  = 4 (integer quotient)

2. Division Assign with float values

In the following program, we shall do division assignment with floating point values.

main.swift

var x: Float = 3.14
var y: Float = 1.2
x /= y
print("x is \(x)")

Output

x is 2.6166666

Explanation

x /= y

x = x / y
  = 3.14 / 1.2
  = 2.6166666

Summary

In this Swift Operators tutorial, we learned about Division Assignment Operator, its syntax, and usage, with examples.