compound assignment operator , swift basic operator , comparison operator , ternary conditional operator , simplyprogrammingIDEA.

# Swift basic operator 

In the last blog we start reading swift programming basic operator , we see all about operator and assignment and arithmetic operator in details. And in this blog we see some remaining operator .

Compound assignment operator 

In compound assignment operator we use ( = ) with other operator.
var a = 5
print(a)     
// 5
a += 2 
the a += 2 is work as a = a + 2 and give the result of a + 2.
print(a)
// 7
a -= 4
print(a)
// 3

Comparison Operators
In swift there is comparison operators to compare . swift support all the standard comparison operaors.
·       (a == b) equal to operator
·       (a != b) not equal to operator
·       (a < b ) less than operator
·       (a > b) greater than operator
·       ( a >= b ) greater than equal to
·       ( a < = b) less than equal to
Swift also provides two identity operators (=== and !==), which you use to test whether two object references both refer to the same object instance.
Each of the comparison operator return the Bool value to indicate the statement  is true or not
1 == 1 // true because 1 is equal to 1
2 != 1 // true because 2 is not equal to 1
2 > 1 // true because 2 is greater than 1
1 < 5 // true because  1 because 1 is less than 5
1 < = 1 // true because 1 is less than or equal to 1.
Comparison operator use in conditional statement. We use frequently comparison operator in if , else conditional statement.
var a = 10
var b = 12
if (a == b){
          print (a + b)
}else{
          print( b – a)
}
Output = 2
// write a program to check the string is equal or not
var name = nitin
var surname = prakash
if ( name == surname ){
          print(“ name and surname is same”)
}else {
          Print(“name and surname is not equal”)
}
output :- name and surname is not equal
We can also compare two tuple if they are of the same type and the same number of values , tuple are compare left to right one value at a time, until find two value that are not equal. Those two values are compared, and the result of that comparison determines the overall result of the tuple comparison. If all the elements are equal, then the tuples themselves are equal. For example
1.    (4, "elephant") < (8, "apple") // true because 4 is less than 8; "elephant" and "apple" are not compared
2.    (3, "apple") < (3, "bird") // true because 3 is equal to 3, and "apple" is less than "bird"
3.    (4, "dog") == (4, "dog") // true because 4 is equal to 4, and "dog" is equal to "dog"
Ternary conditional operator
Ternary operator is used to decrease the line of code the same program we can write with the help of if ,else statement ex:
If (a > b){
          print(a)
}else {
          print(b)
}
The same program we can write with the help of ternary operator in a single line ex : if (a > b) ? a : b
Same output we get the output get with help of that if else statement but this decrease the line of code we use.
Some easy program to practice
(simplyprogrammingIDEA)
# write a program to compare two number and add 5 in the greater number?
var a = 7
var b = 5
if (a > b){
          a = a + 5
          print(a)
}else{
          b = b + 5
          print(b)
}
// ouput:- 10
# write a program to check given number is odd or even through the ternary operator.
var number = 10
if(a % 2 == 0) ? print( a “is even”) : print (a “ is odd”)
// output:- 10 is even.
# write a program to calculate simple interest.
var rate = 4
var principle = 1000
var time = 4
var simpleInterest = (principle * rate * time ) / 100
print(simpleInterest)
output: - 160

Comments

Popular posts from this blog

Simple function to Encryption on Mobile No,email id or any user information in react-native

Counter function for adding item in cart and select the item in the list

React-native pop up ,React-native Pop up hide on the touch of outside the pop up,