Kotlin basics #2

I know you waited for it. KOTLIN BASICS PART 2 🎉 Let’s continue our blog about island in Russia 🇷🇺🪆

Let’s continue our journey 🚌 💨

The entry point of this journey will be an Arrays. Array this is a list of information. For example, if you want to store 5 numbers and to avoid creating 5 variables, we could create a one Array. Let’s begin 🏃‍♂️

Array 🧦

arrayOf()

We create a var. Then a name of this array. Then in this var we use arrayOf() function and pass the item values between the brackets. I create an array with name ourArray and values 1, 2, 3, 5, 7.

In array you can store Strings, Doubles or other data types. But what we can do with arrays. We have learned how to add data in array also we can read this information.

ourArray[0]

If we wanna to read some element from array we need to write the name of this array and then add [ ] this bracket, inside this bracket we need to write a number of elements.

I think you have a question. Why I write 0 inside the brackets? Because arrays start counting elements, not from 1, but from 0. So if we wanna read a first element from array we need text in brackets 0, not 1. If second element — 1, not 2. I know that this is not ordinary, but you will get used to it. Ok, now we have learned an Array, we can continue and go to a Condition.

Conditions ☝️

Condition this a very useful thing in programming. Using conditions we have written a conditions, wow 🤩

If expression

If this is a condition in Kotlin. In our example, we add keyword if than ( ) inside of brackets we need text a condition, and in the end we add { } this kind of brackets. Yep, in programming, we have many types of brackets.

Ok, let’s read this code. If var a will be equal to var b — do something. Also, we have an else expression.

Else is working like if, but else expression start works if our first if not worked. If var a not equals to var b do something else. Ok, now you know about conditions, let’s make something with conditions.

Example 1 🧐

We create two vars and start to compare it. If var a is equal to var b — print “Everything is ok :)”, but if var a not equals var b — print “WHAT?? It is not ok, man, it is not ok!”. If we run this code, we will see this: “WHAT?? It is not ok, man, it is not ok!”, because a not equals to b.

Loops 👌

Now, we have loops. From the title we can understand that we will loop some code. Let’s loooooooop it!

For loop

Here we have a for loop. We write an for keyword than, our little circle brackets ( ) some code in it, and our little don’t circle brackets { }. But what means this ‘some code’ inside circle brackets? Good question!

(i in 1..3). For i in 1 to 3 — do this. i this is an iterator. It is i — count from 1 to 3. We can do some example for understanding.

Example 2 🧐

For i in 1 to 3 — print i. Just run this code and you will see a result of this expression. I hope you see 1 2 3. Yep, now you have a counter in Kotlin! You can change numbers, for example from 2 to 10. Try it :)

WhaT?

Here we have a for loop and Array. Yes, we can print every element of ourArray using for.

We create an array. Than a for loop. In brackets of our loop, we have this code:

i in ourArray.indices. For i in ourArray elements — print ourArray[i], so print the element that have the same position in an array, as our i. Run this code and we will see what happens. In the terminal you should see 1 2 3 5 7.

Let’s finish our journey 🏁 🚌

Oki-Doki, this was a second and the last lesson of Kotlin Basics. I hope you liked it :) Continue learning and maybe you will be a programmer, some day, some day…

Documentation 📜

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store