Learn Kotlin with me Part -2 — Understanding the basics

Hitesh Kohli
4 min readApr 21, 2024

--

More and more developers are switching to Kotlin. It is becoming one of the most popular languages in today’s world. It is because of its features and simplicity. So, let’s try to understand the basics of Kotlin and get good with it.

So, welcome to the series of Learn Kotlin with me. If you haven’t been following, I have written two articles and made two videos here.

Let’s get started with today’s “Learn Kotlin with me” session.

Understanding the basics:

We will understand these topics today.

  • Operators
  • Short Circuiting
  • Conditional Statements
  • When Statements and ranges
  • Loops

Operators:

Operators are the basics that we use in our day-to-day programming. If you want to be good with DSA questions or Leetcode, becoming good with operators is necessary.

Types of Operators:

  • Arithmetic Operators -> -,+,*,%,/
  • Logical Operators -> || ,&&
  • Relational Operators -> <,>,=<,>=
  1. Arithmetic Operators — They help us perform simple arithmetic operations.
  2. Logical Operators — They help us perform Logical decisions. (We will explore them further)
  3. Relation Operators — They help us perform relational comparisons.

Here is a simple example of Relational example and Conditional statements:

fun vehicle(carName:String, carTires:Int,bikeTires:Int,bikeName:String){

if(carTires>bikeTires)
{
println("$carName has $carTires tires")
}
else
{
println("$bikeName has $bikeTires tires")
}

}
vehicle("Mustang",4,2,"Ninja")OUTPUT--->
Mustang has 4 Tires
  • The relational operator(>) compares two integers and gives true or false values to the conditional statements.
  • The conditional Statement decides which print function will run

Here is another example of an addition operator:

fun vehicle(carTires:Int,bikeTires:Int){
println("Vehicles have ${carTires + bikeTires} tires")
}
  • The addition operator adds the two variables and the print function prints the statement

Here is another example of not and And operator:

fun vehicle(carTires:Int, bikeTires:Int){

if(carTires!=4 && bikeTires!=2)
{
println("It is neither a bike nor a car")
}
else
{
println("It is a bike or a car")
}

}
  • The not (!) operator checks that if variables are equal to a variable.
  • The And (&&) operator checks that both the conditions are true. If both conditions are true then it will return true else it will return false.
  • Conditional Statements print the statements according to the operator’s output

We can also understand the OR operator with this function.

fun vehicle(carTires:Int, bikeTires:Int){

if(carTires!=4 || bikeTires!=2)
{
println("It is neither a bike nor a car")
}
else
{
println("It is a bike or a car")
}

}
  • The OR(||) operator checks if at least one condition is true. If it is true then it will return true else it will return false.

I hope this makes operators are little clear to you. Let’s now explore short-circuiting.

Short-Circuiting in Operators

Short-circuiting means that one condition’s result will decide the next condition’s result.

Let’s try to understand this with an example:

var j=10
var i=9
var shortCircuit = j==10 || i++==10
println(shortCircuit)
println(i)
println(j)
OUTPUT -->
TRUE
9
10

As you can see with this example, the condition j is equal to 10 is true. So it never checks the other condition.

Why?

It is because of the OR(||) operator. Even if one condition is true then it will return true. It does not check other conditions. This is called short-circuiting.

When we try to print i it says 9 even if we added 1 in it.

Short-circuiting does not hold with the AND(&&) operator because both conditions’ results should be true or else it will return false.

Example:

var j=10
var i=9
var shortCircuit = j==10 && ++i==10
println(shortCircuit)
println(i)
println(j)
OUTPUT -->
TRUE
10
10

Till now, we have used conditional statements multiple times. So, let’s try to understand conditional statements.

Conditional Statements

Conditional statements are used a lot in our day-to-day programming. So, it is essential to understand them.

Here is an example of if-else statements:

var i=10
var j=9
if(i>j)
{
prinln("i is greator")
}
else if(i==j)
{
println("Both are equal")
}
else
{
println("j is greator")
}
OUTPUT --->j is greator

The if-else if-else condition tries to find the condition which true and returns the answer.

Here is an example of when Statement:

when{
i>j ->{
println("I is greator")
}
i==j -> {
println("Both are equal")
}
else ->{
println("j is greator")
}

The when statement works similarly to if-else conditions.

It is a replacement of the Switch statement in traditional languages.

Ranges

Ranges are important to make loops, they are used to solve DSA problems or Leetcode questions.

Here is an example of the range:

var i=4
if(i in 1..5)
{
println("There are equal to or less than 5")
}
else
{
println("There are more")
}
OUTPUT--->There are equal to or less than 5

In this example, the range of 1 to 5 is set with (..)operators. It means that it tries to see if the variable is greater than or equal to 1 or less than or equal to 5.

Loops

Let’s try to use ranges in loops.

Example:

var j=4
for(i in 0..5){if(j==i){
prinln("There are 5")
}else{
println("There are not")
}
OUTPUT--->
There are not
There are not
There are not
There are not
There are not
There are 5

The for loop runs from 1 to 5 and the if-else statement checks if any number is equal to j.

I hope this makes the Kotlin more clear. We will explore the rest in upcoming blogs. Thank you for reading.

If I have written something wrong, please forgive me. I hope you enjoyed reading.

Thank you,

Your Captain (the commute)

Hitesh Kohli.

Learn more here:

--

--

Hitesh Kohli

Hi, my name is Hitesh Kohli, I work at Geeks for Geeks as an Android developer. I love messing around with apps and games.