Swift: Arithmetic Operators

Arithmetic Operators in Swift:

Let’s use arithmetic operators in Swift to calculate things now that we’ve specified variables.

Some of the basic arithmetic operators are as follows:

  • + addition
  • * multiplication
  • - subtraction
  • % modulo (divides and gives the remainder)
  • / division

Consider the following example:

var points = 0;
// points are 0
 
points = 4 + 6;
// it is now 10
 
points = points - 2;
// it is now 8
 
points = 3 * 2;
// it is now 6
 
points = 8 / 4;
// and now 2
 
points = 5 % 2;
// and now 1

Note: Parentheses can be used to specify the order of operations. For example, in points = 3*(2 + 1), the parentheses make the points equal to 3*3 rather than 6 + 1.

Helpful Tools:

Here are some useful tools to help you along your journey!

Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.

Scroll to Top
%d bloggers like this: