Operator in the swift programmign languaga
#Basic
Operator in Swift Programming.
Operator is special
symbol that is use to change , combine and perform the task on the given value.
For example ( + ) is arithmetic operator use to add two numbers.
Swift gives almost all
the operator that is present in the c programming language. Swift provide the
range operator that is not present in the c programming. 1…2 , a..<b , it is
used to show the range of value.
In this blog we are
going to see common operator and learn
to use them and try to build
programming_idea.
Terminology
There are tree type of
the operator in all the programming language , unary , binary and ternary.
·
unary operator is performed operation on
the single operand.( - a ) , unary prefix operator need just before(!a) the
operand and unary postfix operator need to keep just after the operand(a+)
·
Binary operator need two operand to perform
the operation. It is keep between the two operand ex ( a + b) , ( a – b)
·
Ternary operator need two operand to
perform the operation . ternary operator is used to remove the use of if , else
statement . (a ? b : c)
Assignment
Operator:
Assignment Operator is
use to assign and update the value in
the variable ,
a = 5
b = 6
a = b
// now a is equal to
the 6 because we assign the value of b in the a so the value of a is replace with the value of b.
With the help of
assignment operator we also assign the value of the tuple
Ex –
let (a , b) = (20 , 30)
this is the tuple here
20 assign to the a and 30 to b
in the swift programming
we can not use the assignment operator in the if , else condition statement .
it is strictly prohibited .
if a = b {
}
This is not valid in
the swift programming we you assign value like this it is going to give the
error.
Arithmetic
operator
Swift provide four
standard arithmetic operator for all the numbers in the swift.
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
1 + 2 = 3
10 – 2 = 8
2 * 22 = 44
40 / 5 = 8
Along with this four standard operator there is also a
operator the is reminder ( % )operator that is also present in the c
programming language present in the swift programming language.
Reminder operator is use to get the reminder of the value .
72 % 3 = 0
9 % 2 = 1
Unlike the arithmetic
operators in C and Objective-C, the Swift arithmetic operators don’t allow
values to overflow by default. You can opt in to value overflow behavior by
using Swift’s overflow operators (such as a
&+
b)
Addition operator is
also used for the concatenation of two strings.
“ programming + IDEA” =
programmingIDEA
Unary
( + ) operator
Unary ( + ) operator is
not change the value it is same as with the ( + ) mark there is no difference with
a number having or not having the unary ( + ) operator.
a = 6
a = +6
although the unary (+) don’t
do to anything but we use this when we in the operation there is negative
number present.
Some
basic program to understand the Arithmetic operator:-
Print
the sum of given Two numbers:
var a = 10
var b = 20
print( a + b)
// 20
Subtract
the a from b
Print ( “the value after
subtracting a form b = “b – a)
//the value after subtracting a
from b = 10
Write a
program to find the average of the 5 numbers:
var a = 10
var b = 5
var c = 8
var d = 12
var e = 6
var sum = a + b + c + d + e
var average = sum / 5
print (average)
# program to build the programmingIDEA
Print the value of a
A = 5 + 6 – 4 *2
Print(A)
// 3
Print the value of z
Z = 70 / 5 * 2
Print(Z )
//28
Print the value of I;
I = 14 % 3 + 5 – 2
Print(I)
// 5
This is all about the operator in
swift programming language and this is not the all operator of the swift in
this blog only the assignment and arithmetic operator are cover by me for the further
operator in swift follow my blog.
I write these blog to increase
the ProgrammingIdea of the freshers.
Comments
Post a Comment