String variable
var myVar="Hello world"
var myVar:String
myVar="Hello World"
Integer variable
var MyVar=10
Decimal/Float variable
var myVar=1.0
For constant value variable
val myVar="Helloworld"
myVar="is not changed"
print(myVar)→output is Helloworld
Ranges
Using Double dots
val Myvar=1..10 // ranges from 1,2,3,4,.....9,10
val Myvar=1.rangesTo(10) // ranges from 1,2,3,4,.....9,10
Using downTo
val Myvar=10 downTo 1 //ranges from 10,9,8,7...3,2,1
val Myvar=10.downTo(1) //ranges from 10,9,8,7...3,2,1
Using steps
val Myvar=10 downTo 1 step 2 // ranges fro 10,8,6,4,2
Using alphabets
val Myvar="a"..."z" //ranges from a,b,c,d,.....z
val Myvar='a'...'z' //ranges from a,b,c,d,.....z
Checking a value in range variable
var MyvarinMyvar='a' in Myvar //checking a in Myvar
If else expression
val a=2
val b=3
if(a>b)
c=10
else
c=50
print(c)
or we can define same code in different way
val a=2
val b=3
var c:int=if(a>b)
a
else
b
print(c)
When and case
when(a)
{
1->print("a is 1")
2->print("a is 2")
else->print("a is unknown")
}
FOR LOOP
java
for(i=0;i<10;i++)
{
System.out.println("Hello world");
}
Kotlin
for(initializer in ranges)
for(i in 1..10)
{
println("Hello world")
}
WHILE LOOP
while(i>5)
{
print("Hello world")
i++
}
DO WHILE LOOP
do
{
print("Hello world")
i++
}
while(i>5)
RETURN STATEMENT
fun max(a;int,b int):Int
{
if(a>b)
return a
else
return b
}
or you can define return statement using equal to simply by
fun max(a;int,b int):Int
{
=if(a>b)
a
else
b
}
printing variable
print("a is larger" $a)
Return statements using apply
fun main(args:Array<String>)
{
var man=man()
man.apply{ // Using apply we can avoid man.name="Ameen" and man.age=29
age=29
name="Ameen"
}. Startrun() //here we can run function too in with we can't use this method
//we can also use with as like apply but dot function name can;t be applied at last for running function
man.apply{ // Using apply we can avoid man.name="Ameen" and man.age=29
age=29
name="Ameen"
}
}
class man{
var age:Int=1
var name:String="John"
fun Startrun(){
print("Now my name is $name")
}
}