Swift: Operators - Assignment, Arithmetic, Remainder, Unary Minus

Basic Operators

Operators are symbols that perform operations on values. For example, the + operator performs the addition operation. The * operator performs the multiplication operation.

There are many different types of operators in Swift, including:
  • Arithmetic operators: Arithmetic operators perform mathematical operations on values, such as addition, subtraction, multiplication, and division.
  • Comparison operators: Comparison operators compare two values and return a Boolean value indicating whether the relationship between the two values is true or false.
  • Logical operators: Logical operators combine two or more Boolean values and return a single Boolean value.
  • Bitwise operators: Bitwise operators perform operations on the individual bits of a value.
  • Compound assignment operators: Compound assignment operators combine an assignment operation with an arithmetic or bitwise operation.

Terminology

The following are some important terms to know when discussing operators:
  • Operand: An operand is a value on which an operator operates.
  • Expression: An expression is a combination of operands and operators that evaluates to a single value.
  • Operator precedence: Operator precedence determines the order in which operators are evaluated in an expression.

Assignment Operator

The assignment operator (=) is used to assign a value to a variable or constant. For example, the following code assigns the value 10 to the variable number:

var number = 10

Once a value has been assigned to a variable, the variable can be used to refer to that value. For example, the following code prints the value of the number variable to the console:

print(number) // Prints "10"

Arithmetic Operators

Arithmetic operators perform mathematical operations on values. The following table shows the most common arithmetic operators in Swift:

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Remainder

Example:

The following code shows how to use arithmetic operators to perform simple mathematical operations:

// Addition 
let sum = 10 + 5 // sum is equal to 15 

// Subtraction
let difference = 10 - 5 // difference is equal to 5

// Multiplication
let product = 10 * 5 // product is equal to 50

// Division
let quotient = 10 / 5 // quotient is equal to 2

// Remainder
let remainder = 10 % 5 // remainder is equal to 0 

Remainder Operator

The remainder operator (%) returns the remainder of a division operation. For example, the following code shows how to use the remainder operator to calculate the remainder of dividing 10 by 5:

let remainder = 10 % 5 // remainder is equal to 0

The remainder operator can be useful for checking if a number is even or odd. For example, the following code checks if the number 10 is even:

if 10 % 2 == 0 { 
 print("10 is even.") 
} else { 
 print("10 is odd.") 
}

This code will print "10 is even." to the console, because the remainder of dividing 10 by 2 is 0.

Basic operators are an important part of the Swift programming language. They allow you to perform mathematical operations on values, compare values, and assign values to variables and constants.

By understanding how to use basic operators, you can write more concise and efficient Swift code.

Unary Minus Operator

The unary minus operator (-) is a prefix operator, which means that it is placed before the operand it operates on. It is used to negate the value of its operand, meaning that it changes the positive sign of a value to negative, or vice versa.

The unary minus operator can be used on any type of numeric value, including integers, floating-point numbers, and booleans. It can also be used on the result of an expression.

Here are some examples of how to use the unary minus operator:

// Negate the value of an integer variable 
var number: Int = 10 
number = -number // number is now equal to -10 

// Negate the value of a floating-point variable 
var decimal: Double = 3.14 
decimal = -decimal // decimal is now equal to -3.14 

// Negate the value of a boolean variable 
var isTrue: Bool = true 
isTrue = -isTrue // isTrue is now equal to false 

// Negate the result of an expression 
let sum = 10 + 5 
let difference = -sum // difference is now equal to -15

The unary minus operator can be a useful tool for writing code that is more concise and efficient. For example, instead of writing the following code:

let number = 10 
let negativeNumber = -number

You could simply write the following code:

let number = -10

This is because the unary minus operator is evaluated before the assignment operator.

The unary minus operator can also be used to write code that is more readable and understandable. For example, the following code is more readable and understandable than the following code:

// This code is less readable and understandable:
if number < 0 { 
 // ... 
} else { 
 // ... 

// This code is more readable and understandable 
if -number > 0
 // ... 
} else { 
 // ... 
}

This is because the unary minus operator is used to negate the value of the variable number in the second example, making it clear that the condition is checking if the value of number is greater than 0.

Overall, the unary minus operator is a powerful tool that can be used to write more concise, efficient, and readable Swift code.
Previous: Swift: Error Handling, Assertions and Preconditions, Debugging with Assertions, Enforcing Preconditions     Next: Swift: Operators - Unary Plus Operator, Compound Assignment Operators

Comments

Popular posts from this blog

Swift: Operators: Comparison Operators, Ternary Conditional Operator, Nil-Coalescing Operator

Introduction and get started with Swift