Swift Programming - Zero To Hero - Part Six

Introduction

This is part six of "Swift Programming - Zero to Hero"  series. In this article, we will be learning about Arithmetic Operators. Before you continue in this session, please follow the previous series.
Arithmetic Operators 
 
Almost all the computer programs do arithmetic operations, such as addition, subtraction, multiplication, and division. There are five arithmetic operators, plus(+), minus (-), multiplication (*), division (/), and modulus (%) used in Swift. The plus (+) sign is used for addition, a minus (-) sign is used for subtraction, asterisk (*) is used for multiplication, forward slash (/) is used for division, and modulus (%) is used to return the remainder. Let's see an example code snippet for Arithmetic Operations, 
 
Addition 
  1. 10+25=35  
 Subtraction
  1. 20-10=10  
 Multiplication 
  1. 6*5=30  
Division 
  1. 10/5=2  
 Modulus 
  1. 7%5=2  
Now, let's consider an example where an assignment statement contains more than one arithmetic operator, as shown in the following code snippet,
  1. 5+3*2  
In the preceding code snippet, the answer depends on various things. For example, if you add 5 and 3 first and then multiply the result by 2, the answer is 11. The possibility of such confusion can be avoided by using the rules of precedence. This means that the arithmetic operations are performed in a specified order of precedence, multiplication (*), division (/), modulus (%), addition(+), and subtraction (-).
 
In an assignment statement, the multiplication and division operations are performed first; then, modulus operations, and finally, the addition and subtraction operations. So, the answer to the preceding example would be 11 instead of 1, since the multiplication has been done before the addition. 
 
If the precedence level of the two operators is same, the operations are performed from left to right, as shown in the following example. 
  1. 20/2*5  
In the preceding example, since both the operators have same precedence level,  the division operation is performed yielding the result as 10. Then, the multiplication operation is performed, which results in the final answer as 50. If you want to perform multiplication first and then division, you can use parenthesis so that the operations within the parenthesis can be performed first. You can rewrite the example as follows, 
  1. 20/(2*5)  
In the preceding example, multiplication is performed first and then the division operation is performed that yeilds the final result as 2. There are no limitations on the number of parenthesis to be used, but every left parenthesis needs a right parenthesis. If you type an assignment statement in code editor with unmatched parenthesis, the end of the statement gets squiggled, which indicates an error. When you nest one parenthesis within another parenthesis, the evaluation starts with the innermost set of parentsis and moves outwards, as shown in the following example, 
  1. ((2+3)*5)+7  
In this case, the addition of 2 and 3 is performed first; then the result is multplied by 5; and finally 7 is added to the result. So, the final answer is 32. 
 
Conclusion 

In this article, we have learned about Arithmetic Operators. Hope it was useful. We will learn more concepts in Swift programming in upcoming articles. 


Similar Articles