Numeric Types And Operation In Swift Programming Language

Introduction

In this article, we will see a fundamental concept of all programming languages,  called Numeric types and Operations, and we will also see the all types of operators in programming languages ,and see that these types of operators are very similar to other programming languages.

Manipulating numbers using addition, subtraction and etc. operations is similar in all programming languages.

What is Arithmetic Operator?

When you take two bits of a number and want to solve these bits of data this is called an operation. Think about an arithmetic operator; you take two numbers and apply the sum operation and both the numbers are being added together and you see the result.

Now there are some simple operations which every student will know because this operation is not only for computers but also used in mathematics and nowadays it is used in all fields which are related or not related to computers:

  • Add: +
  • Subtract: -
  • Multiple: *
  • Divide: /

Now we implement this operator into two numbers and see the result --  for this purpose I used Xcode IDE which is an Apple product for software, and iPhone app development.

Code

Code

let add = 2+8
let subtract = 12-2
let multiple = 2*5
let divide = 100 / 10
let Divide : Double = 100.878 / 10


What Is Module Operator?

Module operator is also known as remainder operator when you divide two numbers which are completely divisible you have no problem but when you perform the division on numbers which are not completely divisible a remainder appears so module operator only sees the remainder like this,

Code

What is Increment & Decrement Operator?

Swift has two operators which are similar to other languages. Increment operator in used to increment the value and decrement operator is used to decrease the value. In Swift 3.0 the ++ operator is eliminated by +=1.

Code

Operators with Mixed Values?

Let's suppose you give a salary to an employee  and per hour salary is $53 and there are 31 days  in the month;  if any employee gives only 30 minutes of their time we should pay this, so what would happen? Because we take salary as an integer and now we can’t change the data type of salary which means we are restricting. Now we solve this problem with another way in Swift,

Code

In this example you can see that we convert days into double to make the data type the same.

What are Boolean Operators?

Boolean operators are also the same in all languages, like in Boolean operators we have two values, one is true and the other is false. With the help of Boolean, we check over a condition which is true and false like this:

Code

The Boolean logic is so simple  -- if we take  && operator  it means that both conditions are true and if we take || operator it means only one condition is true like this,

Code

The firstname is changed and it is not the same as I declare in a variable so it prints  else statement because only one value is true and in Swift it is mandatory that both values are true

Code

In || operator it's mandatory that only one condition is true and  you can see that lastname is the  same as declaration and it prints True.

This is the end of  the article  -- this is very a basic and common operation in all programming languages and if you have any confusion regarding this topic you can easily ask me.