Control Flow Statements in Kotlin

Contents

keyboard_tab

The course is part of this learning path

If-Else Statement
Overview
Difficulty
Intermediate
Duration
23m
Students
11
Ratings
5/5
starstarstarstarstar
Description

In this course, you will learn about control flow statements in Kotlin, including the if-else statement, the if-else-if ladder statement, the nested if statement, and when statements.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.

Prerequisites

This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.

Transcript

Well, hello everyone. So, in this next section we're going to learn the fundamentals of Kotlin. We're going to learn decision making statements such as the if statement, the when statement. Then,

we're going to learn looping statements, such as for, for-each, while, and do-while loops. Very cool, because decision making structures have one or more conditions to be evaluated or tested by the program. So, if you look at the diagram, I'm going to show you if some condition occurs, check condition. If the condition is true, execute conditional code. If conditional is false, pass conditional code and continue. Now, there are two types of decision making statements in Kotlin. They are if statements and when statements. So, the if statement, this is used to test the condition. So it will check the Boolean condition, true or false. And there are various types of if statements in Kotlin. We could easily classify these statements as if statement, if-else statement, if - else if ladder statement, and a nested if statement.

So, why don't we take a closer look at the if statement and if-else statement syntax? All right, so first, the Kotlin if statement will test the condition. If the condition is true, it executes the if-block. Secondly, the Kotlin if-else statement also tests the condition. If the condition is true, it executes the if-block, otherwise the else-block gets executed. So, why don't we do some examples with this if-else statements in Android Studio? So, what do we do? First create a new Kotlin file, right click on the package name, select new Kotlin file. After selecting the 'File' option, I will name it IfElseStatement. Hit 'Enter', create the main method, write 'main', select the 'maina' option here, hit 'Enter', and that way we get our main method created. So, let's do an example like this. Let's ask the user to enter a number into the console. Then we'll let the user know whether this number entered by the user is an odd number or an even number. So, first we'll define what the user needs to do. So, I'll just write "Please enter a number" inside the print() method.

So, now we'll pass that number that the user enters to the console into a variable. So, here I am typing 'var number: Int = readLine()' method. Now I'll put two exclaimatin marks at the end of the readLine() method. As I mentioned in the previous lesson, you remember the data that we received from the user is always of the string type? So, that means we have to convert it to the type integer. So, here I'll write '.toInt()'

Okay, so we took the number that the user entered in the console, converted it to an integer, and passed it to a variable named number. All right, so now, let's check to see whether the number is even or odd. So, we can tell the number is even or odd by dividing it by two, right? So, if the remainder is zero when we divide the number by two, it is an even number, if the remainder is one, it is an odd number. Okay, so we'll check this situation using the if statement. So here, I'll write if and choose the if option suggested by the editor.

So, after writing number in parenthesis, I'll use the % sign and write '2'. Now, do you remember that we were able to find the remaining number thanks to the % sign? So, if the remainder is zero, then the number is even. So, now I'll open up a curly bracket and press 'Enter'. And this is the way that we define the scope of the if structure. Now between these curly brackets, we must define the operations that should be performed when the number is even. So, in this example, we will print to the console that the number is an even number. So that's why I'll write 'sou' and 'press 'Enter' . Then I'll write the number after the $ sign in parenthesis. And I'm writing '"$number is an even"'. So, now let's run and test our code. Okay, so in the console screen, let's enter '8'. Eight is an even number, so the condition is true and the codes inside the body of if get executed. Okay, cool. So, let's run the code again. And now on the console screen, let's enter '7'.

Seven is an odd number, so the condition is false and the codes inside the body of the if are not executed. See how that works, that's very cool, and of course it gets way more complex. But first, I want to show you how to add an else statement to this code. So, if the condition is evaluated to be false, the else statement gets executed. So, we add print method that includes "number is odd" text in the else block. Okay? So, now let's run the code. And on the console screen, we'll enter '12', so we get 12 is even and that's the message from the if statement. So, that works. So, now let's run the code and enter '13' and we get the 13 is odd message from the else statement. So, there you go my friends. That is how we use the if-else statement. So, we're going to build on this from here because in the next video we're going to learn all about the if - else if statement, as well as my favourite and might become yours, the nested if statement. All right. So, I'll see you in the next video.

 

About the Author
Students
170
Courses
23
Learning Paths
1

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics