Mad Kotline
Mad Kotline
Environment Setup
The following steps are required to complete to setup kotlin environment in your device.
First App
It is suggested to use notepad to type your kotlin program. The kotlin file should be saved using
extension.kt. The main function is required to start the execution.
fun main() {
println("Hello World")
}
Keywords
break do in
continue for var
val while this
null throw when
try true false
Identifiers
num x2 x_c
q5 test NUM
• Creating an Arrays
val x = arrayOf(90,15,60,10) // type of array is not fixed
val x = arrayOf(‘A’,15,2.5f)
val x = arrayOf<Int>(90,15,60,10) // type of array is fixed
• Creating a String
val info: String = "NIBM"
Attributes
length Represents the length of string
lastIndex Represents the index of last character in string
Methods
first() Returns first character in the string
last(): Returns last character in the string
compareTo(string) compare strings equality
capitalize() Returns a copy of this string having its first letter
uppercased
toLowerCase() Returns a copy of this string converted to lower case
Data Operators
Arithmetic Operators
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Division x/y
% Modulus x%y
Comparison Operators
< Greater Than x>y
> Less Than x<y
<= Less Than or Equal x<=y
>= Greater Than or Equal x>=y
== Equal x==y
!= Not Equal x!=y
Assignment Operators
= Assignment x=10
+= Add and Assign x+=10
-= Subtract and Assign x-=10
*= Multiply and Assign x/=10
/= Divide and Assign x/=10
%= Modulus and Assign x%=10
Unary Operators
-- Decrement x--, --x
++ Increment x++, ++x
Logical Operators
&& And x<a && x>b
|| Or x<a || x>b
! Not !x
Selections
Syntax if:
if(x>10)
{
}
else if(x>5)
{
}
else
{
}
Syntax when:
val x: Int = 10
when(x) {
10 -> println("A")
20-> println("B")
30 -> println("C")
40 -> println("D")
50 -> println("E")
else -> println("F")
}
Repetitions
Syntax while:
var counter = 1
var counter = 1
do{
println(counter)
counter ++
}
while (counter <= 10)
Syntax for:
for (i in 1..3) {
println(i)
}
Syntax repeat:
repeat(4){
x ->
println("This statement will be printed 4 times!!")
println("Index is: $x")
}
Syntax:
display(50)