Control Flow Statements
The course is part of this learning path
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.
This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.
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.
Well, hello, my friends. So, right now we're going to take a closer look at this If-else-if. We call it a ladder statement and a nested if statement. We're going to take a look at the syntax. So, first the Kotlin if-else-if ladder statement executes one condition from multiple statement. And secondly, the nested if statement represents the if block within another if block. So, here the inner if block condition executes only when the outer if block condition is true. Alright, let's do some examples with an if ladder and nested if statement. First up, what do we do? Create a new Kotlin file, right click on the package name, select the new Kotlin file option. So I'll determine the file name and here I'll just call it IfLadderNestedif. 'Enter' and create a main method. Write
main, select 'main a' option, hit 'Enter' and there is the creation of the main method. Alright, so we're going to show a message to enter age by using the print method. And we will declare a variable, your age with intent type and assign the readLine method to this variable. Also, I'm going to convert to the integer using the toInt method.
Okay so, let's start to write the if statement. If your age is less than 13, then we will print "You are a child" to the output, else if your age is less than 19, we will print 'You are a teenager' to output. If your age is greater than 19, else statement is executed and we will print 'You are an adult' to output. If all test conditions are false, codes inside the body of else is executed, okay? So, let's run the code. So, back over on the console screen. Let's 'Enter' eight, eight is less than 13. So, first if statement is executed, output is "You are a child." Cool. So, let's run the code again and let's 'Enter' 15. The program checks the first if statement 15 is greater than 13 so, the first if statement is not executed. So, the second if statement is checked, 15 is less than 19 and yes, this condition is true so, this statement is executed. So, then the output is "You are a teenager." Alright, so let's run the code again. Let's 'Enter' 45 and the program checks the first if statement, 45 is greater than 13. So, the condition is false. The program then checks a second if statement, 45 is greater than 19 so, the condition is false. The else statement gets executed so, the output is "You are an adult."
Now, I want to add a nested if statement in the else statement, you'll see. So, if your age is less than 65 then, we will print "You are an adult" to output. We add the else statement means that if your age is greater than 65, we will print "You are a senior" to output. Okay. So, let's run the code. And in the console screen let's write 56, else statement gets executed because 56 is greater than 13 and 19 and the else statement 56 is less than 65. So, the if statement is executed and the output is "You are an adult." But now let's run the code again and we'll 'Enter' 68. In the else statement, 68 is greater than 65. So, the else statement gets executed and the output is "You are a senior." You see how that works.
So, let's write a Kotlin program to find the largest of three numbers and implement a nested if statement. You're with me? So, first of all, let's convert the lines of code that we wrote here to the comment lines. And we'll show a message to 'Enter' three numbers by using the print method. So, let's declare four variables whose names are number1, number2, number3 and largest number with the integer type. And resign readLine.toInt methods to these variables. Now look, I'm not going to set the initial value of the largest number variable. Instead, I will pass the largest of the three numbers entered by the user into this largest number variable. So, if you're within the bounds of a function, you may not be able to initialize variables. We're going to cover that topic a little later in the next lessons but let's start to write the statement. So, if number1 is greater than or equal to number2, this is outer if. So, we add a nested if statement. If number1 is greater than or equal to number3, then assign number1 to largest number. Now you're getting it, else assign number3 to the largest number. Alright, so why don't we continue with the outer else statement? We'll add a nested if statement again. If number2 is greater than or equal to number3, then assign number2 to largest number, else assign number3 to largest number. You're cooking with gas now. So, let's print the largest number to the output by using the print method. Okay.
You ready to run the code? So, in the console screen, we'll 'Enter' six, five and 12. So, the program will check the outer if first, six is greater than five, the condition is true so, outer if gets executed. In the outer if, the program checks the inner if, six is less than 12, the condition is false. So, inner if is not executed and the else statement gets executed. And 12 is printed as the largest number. Very cool. So, let's, let's run the code again. But this time we're going to 'Enter' different numbers. How about -2, 1, and 0? And as you can see the largest number is one.
So, what do you think of that? I think it's pretty cool. So, thanks, for sticking with it. And that's enough of the example stuff, right? At least to understand the if statement. To make it clear though, you got to do some more exercises. So, stay tuned for the next video. See you there.
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.