This course takes a deep dive into Kotlin operators, giving you a practical understanding of how to use operators in your code.
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.
Well, hello everyone. All right so now, it's time to talk about the conditional operator. So, conditional operators are used of course for checking multiple conditions together. So, let's try to understand conditional operators and how they function in Kotlin. So, the conditional AND operator, this operator is represented by the symbol double &&. So, in this operation, the result is true, if both operands are true. Now, this operator doesn't check the second condition if the first condition is false. Makes sense, right? So, it only checks the second condition if the first one is true.
Then we have conditional OR operator. So, this operator is represented by this symbol ||. It's called a double pipe. So, in this operation the result is true, if either of the operands happen to be true. So, this operator doesn't check the second condition if the first condition is true, it checks its second condition only if the first one is false. So, why don't we do some examples with conditional operators in android studio and you'll see how we can use them to our benefit. So, you recognize this? I'll first create a new Kotlin file, right click on the package name, select the new Kotlin file option. After selecting the file option here, I'll determine the file name and let's call it conditional operators, enter key and now I must create the main method. So, I'll write main, select the maina option here and hit enter and that creates our main method.
Okay so, let's start to code. We will declare three variables with int type, firstly, number one, assign five; second, number two and assign eight and last number three and assign 12. So, let's declare a variable result with a Boolean type. The default value for Boolean will be false. So, here we're going to use the conditional AND operator and assign the result to the variable result. So, if number one is greater than number two and number three is greater than number one, then the result is true, else the result is false. So, in order to get the result true in the conditional AND operator, all Boolean expressions must be true. So, let's print the result value by using the print method.
So, let's run the code. So, you can see in the console the result equals false because number one is not greater than number two. Okay so, we use the conditional OR operator and assign the result to the variable result. If number one is greater than number two or number three is greater than number two then the result is true, else the result is false.
So, in order to get the result true in the conditional OR operator. Well, at least one expression must be true. Okay, so now let's run the code. And you see it in a console, the result equals true because number three is greater than number two. So, that's how you use conditional operators in Kotlin. So, we'll take a short break here and then in the next video, we're going to deal with operator precedence. Yes, it just gets deeper folks, but it gets a whole lot more fun too. So, I'll see you in the next one.
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.