Monday, 11 December 2017

Kotlin: if-else if-else

By using if-else if-else construct, you can choose between number of alternatives.

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions.

IfElseIf.kt
fun main(args: Array<String>) {
 var marks: Int = 65

 if (marks < 35)
  println("You are failed");
 else if (marks < 50)
  println("You are passed and got third class");
 else if (marks < 60)
  println("You are passed and got second class");
 else if (marks < 70)
  println("You are passed and got first class");
 else
  println("You are passed and got distinction");
}

Output

You are passed and got first class


Previous                                                 Next                                                 Home

Sunday, 10 December 2017

Kotlin: if-else statement

If the condition true, then if block code will execute, otherwise else block code will execute.

Syntax:
if(condition){

}else{

}

IfElse.kt
fun main(args: Array<String>) {
 var marks: Int = 65

 if (marks < 35) {
  println("You failed in the Exam. Better luck next time");
 } else {
  println("You passed in the exam")
 }
}

Output

You passed in the exam



Previous                                                 Next                                                 Home

Saturday, 9 December 2017

Kotlin: if statement

"if" tell the program execute the section of code when the condition evaluates to true.

Syntax
if(condition){
         // Executes if the condition true
}

IfStmt.kt
fun main(args: Array<String>) {
 var marks: Int = 20

 if (marks < 35) {
  println("You failed in the Exam. Better luck next time");
 }

 marks = 55

 if (marks < 35) {
  println("You failed in the Exam again. Better luck next time");
 }

}

Output
You failed in the Exam. Better luck next time


Previous                                                 Next                                                 Home

Friday, 8 December 2017

Kotlin: Control flow statements

Kotlin provides control flow statements
     for decision making (if, if-else, when) and
     for looping (for, while, do-while)

Decision Making statements
a.   if statement
b.   if-else statement
c.   if-else if-else ladder
d.   when statement

Looping statements
a.   for
b.   while
c.   do-while


Previous                                                 Next                                                 Home

Thursday, 7 December 2017

Kotlin: === vs ==

Kotlin supports two types of equality.
a.   Referential Equality
b.   Structural Equality

Referential Equality (===)
 ‘===’ operator is used to check, whether two references point to same object or not.

Ex:
ref1 === ref2

Above statement returns true, if references ‘ref1’ and ‘ref2’ points to same object, else false.

Structural Equality(==)
Structural equality is used to check, whether the values of both references are equal (checks by using equals() method of java).

Ex:
ref1 == ref2

Above statement returns true, if ref1.equals(ref2) return true, else false.

Equality.kt

fun main(args: Array<String>) {
 var intVar1 : Int = 10000
 var intBoundedVar1 : Int? = intVar1
 var intBounderdVar2 : Int? = intVar1
 
 println("(intBoundedVar1===intBounderdVar2) : ${(intBoundedVar1 === intBounderdVar2)}")
 println("(intBoundedVar1==intBounderdVar2) : ${(intBoundedVar1 == intBounderdVar2)}")
}

Output

(intBoundedVar1===intBounderdVar2) : false
(intBoundedVar1==intBounderdVar2) : true


Previous                                                 Next                                                 Home

Wednesday, 6 December 2017

Kotlin: Logical Operators

Kotlin supports logical operators (also called as Boolean operators), to combine multiple conditions. 
a.   || – lazy disjunction
b.   && – lazy conjunction
c.   ! - negation

1. || – lazy disjunction
operand1 || operand2: It returns true, if any of the operand evaluates to true, otherwise returns false.

Operand1
Operand2
Evaluates To
False
false
false
False
true
true
True
false
true
True
true
true

BooleanOr.kt
fun main(args: Array<String>) {

 var operand1: Boolean = false
 var operand2: Boolean = false
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")

 operand1 = false
 operand2 = true
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")

 operand1 = true
 operand2 = false
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")

 operand1 = true
 operand2 = true
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")
}

Output
false || false : false
false || true : true
true || false : true
true || true : true

Why || is called as Lazy Disjunciton?
If the first expression evaluates to true, then Kotlin won't evaluates the entire expression. So Logical OR is called short circuit OR (or) Lazy disjunction operator.


LazyDisjunciton.kt

fun main(args: Array<String>) {

 var a : Int = 10
 var b : Int = 21

 if ((a < b) || (a++ < b)) {
  System.out.println("This statement evaluated");
 }
 System.out.println("a is not incremented " + a);

 if ((a > b) || (a++ > b)) {
  System.out.println("This statement is evaluated");
 }
 System.out.println("a is incremented " + a);

}

Output

This statement evaluated
a is not incremented 10
a is incremented 11

&& – lazy conjunction
operand1 && operand2: It returns true, if both the operand evaluates to true, otherwise returns false.

Operand1
Operand2
Evaluates To
False
false
false
False
true
false
True
false
false
True
true
true


BooleanAnd.kt

fun main(args: Array<String>) {

 var operand1: Boolean = false
 var operand2: Boolean = false
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")

 operand1 = false
 operand2 = true
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")

 operand1 = true
 operand2 = false
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")

 operand1 = true
 operand2 = true
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")
}


Output

false && false: false
false && true: false
true && false: false
true && true: true

Why && is called Lazy conjunction operator?
If the first statement in the expression evaluates to false, then kotlin won't evaluate the entire expression. So, Logical AND is called short circuit AND.


LazyConjunction.kt

fun main(args: Array<String>) {

 var a: Int = 10
 var b: Int = 21


 if ((a > b) && (a++ > b)) {
  System.out.println("This statement not evaluated");
 }
 System.out.println("a is not incremented " + a);

 if ((a < b) && (a++ < b)) {
  System.out.println("This statement is evaluated");
 }
 System.out.println("a is incremented " + a);

}

Output

a is not incremented 10
This statement is evaluated
a is incremented 11


3. Logical not (!) Operator
If the operand is false, ! Operator evaluates it to true
If the operand is true, ! Operator evaluates it to false
Operand
Evaluates To
False
true
True
false


LogicalNot.kt

fun main(args: Array<String>) {

 var operand1: Boolean = false
 var operand2: Boolean = true

 println("operand1 : ${operand1}")
 println("!operand1 : ${!operand1}")
 println("operand2 : ${operand2}")
 println("!operand2 : ${!operand2}")

}

Output

operand1 : false
!operand1 : true
operand2 : true
!operand2 : false



Previous                                                 Next                                                 Home

Tuesday, 5 December 2017

Kotlin: Bit wise operators

Below table summarizes the bit wise operators supported by Kotlin.

Operator
Description
Java Equivalant
shl
Perform signed shift left
<< 
shr
Perform signed shift right
>> 
ushr
Perform unsigned shift right
>>> 
and
Bitwise and
&
or
Bitwise or
|
xor
Bitwise X-OR
^
inv()
Bitwise inversion
~

Other than inv(), all other operators are used in infix form.

Ex:
var1 shl var2
var1 or var2

1. shl: Perform signed shift left
Left shift operator shifts all the bits to one bit left, i.e, it doubles the value.

Syntax:
    variable shl n

    left shift the variable by 'n' number of times.

Variable shl n is equals to variable = variable * 2 power n

        var var1 : Int = 2
        var1 shl 1 = 2 * 2 power 1 = 4
        var1 shl 2 = 2 * 2 power 2 = 8
        var1 shl 9 = 2 * 2 power 9 = 1024

LeftShift.kt
fun main(args: Array<String>) {

 var var1: Int = 2

 var res1 = var1 shl 1
 var res2 = var1 shl 2
 var res3 = var1 shl 9


 println("res1 : ${res1}")
 println("res2 : ${res2}")
 println("res3 : ${res3}")
}

Output

res1 : 4
res2 : 8
res3 : 1024

2. shr: Perform signed shift right
Right shift operator shifts all the bits to one bit right, i.e, it half (divide by 2) the value

   Syntax:
   variable shr n
      right shift the variable by 'n' number of times.

   Variable shr n is equals to variable = variable / 2 power n

   var a : Int = 128
   a shr 1 = 128 / (2 power 1) = 64
   a shr 2 = 128 / (2 power 2) = 32


RightShift.kt
fun main(args: Array<String>) {

 var a: Int = 128

 var res1 = a shr 1
 var res2 = a shr 2
 var res3 = a shr 3


 println("res1 : ${res1}")
 println("res2 : ${res2}")
 println("res3 : ${res3}")
}

Output

res1 : 64
res2 : 32
res3 : 16

3. ushr: Unsigned right shift
Shifts all the bits to one bit right, and make the left most bit to zero

Syntax
Variable ushr n


UnsignedRightShift.kt
fun main(args: Array<String>) {

 var a: Int = -128

 var res1 = a ushr 1

 println("res1 : ${res1}")
}

Output

res1 : 2147483584

Explanation
-128 in 2's complement form is

11111111 11111111 11111111 10000000

>>> shifts the one bit right and replace leftmost bit by zero

so (-128 ushr 1) is like below sequence

01111111 11111111 11111111 11000000

4. Bit Wise and, or, xor
Bit1
Bit2
and
or
xor
0
0
0
0
0
0
1
0
1
1
1
0
0
1
1
1
1
1
1
0


HelloWorld.kt
fun main(args: Array<String>) {

 var a: Int = 8
 var b: Int = 9

 println("a and b is ${a and b} ")
 println("a or b is ${a or b} ")
 println("a xor b is ${a xor b}")
}

Output

a and b is 8
a or b is 9
a xor b is 1


Explanation
   a    = 0000 1000
   b    = 0000 1001
   a&b = 0000 1000

   a    = 0000 1000
   b    = 0000 1001
   a|b = 0000 10001

   a     = 0000 1000
   b     = 0000 1001
   a^b = 0000 0001

Note:
Bit wise operators are applicable only to integer values like byte, short etc., these are not applicable to float, double, Boolean.

5. inv(): Bitwise inversion
Inversion operator change the bit value to zero, if it is one and change the bit value to one if it is zero.

Example
var a : Int = 8
a in binary format is              0 0 0 0 0 1 1 1
a.inv() in binary format is      1 1 1 1 1 0 0 0


Inverse.kt

fun main(args: Array<String>) {

 var a: Int = 8

 println("Value of a is : ${a}")
 println("inversion of a is : ${a.inv()}")
}


Output

Value of a is : 8
inversion of a is : -9



Previous                                                 Next                                                 Home