2

How did the giants rise? Episode II

 1 year ago
source link: https://medium.com/@mselimozen07/how-did-the-giants-rise-episode-ii-5c8efb3e138b
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
1*Sr41AwxMn0Kxy3UgxZlLZA.jpeg

Kotlin: Input, Casting, Operators

In the previous episode, I told you about simple data types. In that episode, we always defined the data ourselves, changed it ourselves, and at the end, we talked about operators. In this episode, we will see very simply getting data from the user (terminal), data conversion and operators.

Input

Most of the time we write code for clients. Depending on the feedback we receive from them, we either continue with my transactions or not. Although there are many different methods for Android developers, there are also very simple ways to get input from client. Those who have worked with Java before are familiar with these methods. You can perform these operations with the scanner and bufferredreader classes in Java.

var sc = Scanner(System.`in`)

Then we create a variable and ask the client to assign a value to this variable with the object we created from the Scanner class.

var name = sc.next()
println(name)

For example, in this example, we have succeeded in getting a name from the user.

Output:

1*XYKPWgW4LvD5zKPBheCxUA.gif

We just got a data type of string type. Can we get other types?

var age = sc.nextInt()
println(age)

This way we can get other types of data as well. You can even make transactions with these inputs if you want.

var age = sc.nextInt()
println(age + 10)

Well, let’s say you received integer type data from the user, but you need string type data for your operations. After this point, we can start learning casting processes.

var age = sc.nextInt()
var intAge = age.toString()
println("My age ${intAge}")

In fact, I have never come across such an example. Usually the opposite happens. How Does? If you design an application, usually the input you get from the user comes as a string and you have to convert it to an integer. Can you do it? Of course!

var stringAge = sc.next()
var intAge = stringAge.toInt()
println(intAge)

There will be no surprises after strings, but you can convert fractional numbers to integers.

var double = 11.5
var inttoDouble = double.toInt()
1*TnhDi-G_a7_PP0G02jEAIA.png

As you can see, our double number has been converted to an integer. Now we can move on to the operators. You can do simple arithmetic with kotlin.

var sum = 25 + 5
var minus = 25.minus(5)
var multip = 5 * 5
var division = 30.div(6)

In these codes, I wanted to show you that you can do arithmetic operations with 2 different methods and also we have assignment operators.

var num = 15
num += 1
//Or you can use it like that,
var num2 = 20
num -= num2

Without further ado, I’d like to move on to the comparison operators with the multiplication and division operations. You can guess how to do multiplication and division. Without further ado, I want to move on to comparison operators. It will be healthier for you to read the explanations between the codes in these sections.

// == means equal
println(4 == 6)
Output: false// != means not equal
println(4 != 6)
Output: true// > greater than
println(14 > 5)
Output: true// < less than
println(5 < 20)
Output: true// >= greater than or equal
println(10 >= 3)
Output: true// <= less than or equal
println(10 <= 3)
Output: false

Why are these operators so important?*

Our next topic is logical operators. Let’s say we have 4 variables.

val var1 = 20
val var2 = 12
val var3 = 45
val var4 = 75
println(var1 > var2 && var4 > var3)

If we use the and(&&) operator will return true if both of our conditions are true.

Output: true

returns false if one of them is false. If we use the or (||) operator, it will return true if either one is true.

println(var1 < var2 || var4 > var3)

We have come to the end of a chapter with this subject. See you soon!

Resources:

Cover Photo: https://tr.pinterest.com/pin/141230138300769505/

https://www.udemy.com/course/android-o-mobil-uygulama-dersi-kotlin-java/

After Script

While talking about assignment operators, I asked why these issues are important. If-else controls, one of the most important issues of programming, are entirely based on these issues. Let me explain what I mean with an example, and let’s take a quick look at the next article.

Let’s say you have an application and it can be used by people over the age of 18. You should learn the age of the user, then act according to the age of the user. Let’s take a simple example.

val sc = Scanner(System.`in`)
var userAge = sc.nextInt()
var legalAge = 18
if(userAge >= legalAge){
println("Welcome to app")
}else{
println("You can't the use app")
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK