Subscribe Now

* You will receive the latest news and updates on your favorite celebrities!

Trending News

Blog Post

Kotlin variable declaration, conditional statement and looping

Kotlin variable declaration, conditional statement and looping

What is Kotlin?

Google has announced Kotlin as a official language for Android Application Development in Google I/O 2017. Google surprised all android developers by this announcement. Than many developers has started work with Kotlin and fall in love with its concise syntax and safety. I am one of them.

Kotlin is a statically-typed language, developed by Jetbrains. With features like higher-order functions, lambda expressions, operator overloading, string templates, and more, Kotlin has a lot more to offer than Java. Because Java and Kotlin are highly interoperable, they can be used together in the same project.

Every programmer knows that for learning any programming language we need to learn syntax of that language and basics of that language like variables, conditional statements, functions. Here in this blog we will learn basics of Kotlin language.

How Kotlin works with Android?

Kotlin and JVM


How to declare variables in kotlin?

Variables can be declare using ‘val’ and ‘var’ keyword in kotlin. Now question is what is difference between ‘val’ and ‘var’.

1). val: val is same as the final modifier in java. As you should probably know that we can not assign to a final variable again but can change its properties. so Val is used to declare immutable variable.


val str:String = "Hello"

// now if you try to reassign value to "str"
str = "World" // this will show you error "val cannot be reassigned"

2). var: we can change the value of variable which is declared using ‘var’ keyword. So var is used to declare mutable variable.


var str:String = "Hello"
//we can change value of “str” if it is defined using ‘var’ keyword.
str = "World" //This will not generate any error

Some more examples for variable declarations:

val a: Int = 1 // readonly value
var x = 5 // `Int` type is inferred
var isSelected = false // Boolean variable
var isSelected:Boolean = false

Conditional statements:


Kotlin if else ladder when


1. Simple If case:

var isSearching=false
    if(!isSearching)   isSearching=true

2. if else:

var maxValue: Int
val pValue=10
val qValue= 20
if (pValue> qValue) {
    maxValue = pValue
} else {
    max = qValue


Make a note that if you try to assign value to variable using if expression than compulsory you have to add else branch.

Note: Kotlin not support ternary operator (condition ? then : else) because kotlin if expression works fine in this case also.


val greater=If(10>20) true else false


When expression:

> When expression in kotlin has replace “Switch Case” which we are using in java and C like language. It matches its arguments with all branches sequentially until some branch condition is satisfied. Here, we can use when expression as a statement or as a expression. If it is used as an expression, the value of the satisfied branch becomes the value of the overall expression. If it is used as a statement, the values of individual branches are ignored(same as if condition).


private fun getRandomNumberInRange(min: Int, max: Int): Int {
    if (min >= max) {
        throw IllegalArgumentException("max must be greater than min")
    val r = Random()
    return r.nextInt(max - min + 1) + min
var mNumber=getRandomNumberInRange(0,10)

1). Simple when expression:

	0-> print(“Number = 0”)
	1-> print(“Number = 1”)	
	else-> {
		print(“Number = None”)

Here else is same as default case in Switch statement. If when is use as expression than else in mandatory.

2). when with range or collections:

	0..5-> print(“Number = 0”)
	6-> print(“Number = 1”)	
	else-> {
		print(“Number = None”)

3). When using branch combined with comma:

	0,1,2,5-> print(“Number = 0”)
	6-> print(“Number = 1”)	
	else-> {
		print(“Number = None”)

4). When using smart cast:

fun getGender(isMale:Any)=when(isMale){
is True-> “Male”
Else-> “Female”

5). We can also replace when instead of if else if chain:

	mNumber.isOdd()-> print(“x is odd”)
	mNumber.isEven()-> print(“x is Even”)
	else->print(“x is not a Number”)

For Loop:

> For loop in kotlin is equivalent to the foreach loop.


for (item in collection) print(item)

for(I in 1..10){

//With array
for(i in arrayNumber.size){

While loop:

while and do..while work as usual

while (x > 0) {

do {
    val y = retrieveData()
} while (y != null) // y is visible here!

Hope you like this blog. If you have any question please ask me in comments or you can message me on twitter and facebook also.


Popularity of kotlin is getting increase day by day. More people are now learning kotlin for android development and most of them giving positive reviews. Most important point is Kotlin is designed with Java Interoperability in mind which makes it unique and easy to learn.

Related posts


  1. Avatar

    So put your own unique spin regarding your content. You can develop good content by doing the particular sphere of the website.

    Michael Jordan didn’t shoot slam dunks the period he collected a b-ball.

  2. Avatar

    I’m truly enjoying the design and layout
    of your blog. It’s a very easy on the eyes which makes it much
    more enjoyable for me to come here and visit more often. Did you hire
    out a designer to create your theme? Exceptional work!

    1. Lokesh Desai
      Lokesh Desai

      no i made it my self

  3. Avatar

    I like the valuable information you supply for your articles.
    I’ll bookmark your blog and take a look at once
    more right here frequently. I am quite sure I will be told many new stuff proper here!
    Best of luck for the next!

  4. Avatar

    I am interested about cotlin

    1. Lokesh Desai
      Lokesh Desai

      ok i will try to add more blogs for kotlin

  5. Avatar
    newtown casino pc

    With havin so much conttent do you ever run into
    any problems of plagorism or copyright violation?
    My site has a lot of completely unjque content I’ve either created myself or outsourced bbut it appears a lot of it is poppingg
    it up all over the web without my permission. Do
    you know anny ways to help stop content from being stolen? I’d certainly
    appreciate it.

    1. Lokesh Desai
      Lokesh Desai

      no i don’t know any way to stop content from stolen. i will research on that and let you know soon

  6. Avatar
    foloren torium

    Your place is valueble for me. Thanks!…

  7. Avatar
    foloren torium

    whoah this blog is excellent i love reading your articles. Keep up the good work! You know, many people are hunting around for this information, you can aid them greatly.

Leave a Reply

Required fields are marked *