5. Advanced control flow in Kotlin | Kotlin Beginners Course

5. Advanced control flow in Kotlin | Kotlin Beginners Course


Hello, welcome to BackToCoding. Today, we’ll be talking about the “Kotlin’s
Advanced Control Flow” Before we get started, don’t forget to hit
that subscribe button & the bell icon so that, whenever I publish a video, you get notified
instantly about it. In the previous video we saw basic control
flow. In this video, we’ll see Advanced control flow. First we’ll talk about a very good feature
of Kotlin, which is “Ranges”. Range is nothing but an interval between two
values. So with the help of Range we can generate our own set of values, on or between the specified
range. Again there are two types, closed range and
half-open range. Closed range is used something like this where the range goes from 0 to 20 inclusive.
That means the numbers are ( 0, 1, 2, 3, 4, 5, …. till 20 ). And in half-open range we use until keyword
It is used like this where the range goes from 0 up to, 19, not
including 20. That means the numbers are (0, 1, 2, 3 till 19) With .. and until operators, we can have a
range in increasing order, but what if we want it in decreasing order. So in such scenarios we have downTo,with the
help of which we create a range in decreasing order. val decreasingRange=20 downTo 0
Here, the range will include ( 20, 19, 18, 17, 16 till 0). Let’s see them practically As you can see here, we’re getting the values
from 0 till 20 Here, we are getting the values only till
19 And here, we’re getting the values from
20 all the way down to 0 Second, we’ll see another type of loop which
is very commonly used several times, i.e. “for loop”. for loop iterates through the entire set of
values in the specified range. The syntax for “for loop” is mentioned
here: So, for loop starts with the for keyword,
followed by a variable which acts as a constant(here we’ve item), followed by in keyword, and
then we mention the range in which it loops(which here is collection). For eg. As we can see here, the for loop has worked
properly and we have got our answer. Here, what exactly is happening is, with the
help of for loop we go through the range starting with 1 till the value of the variable “loopTill”
which in our case is 20. So, in the first iteration, i’s value will
be 1 and for each iteration(i.e. for each cycle) the value of i will be incremented
by 1, until it is equal to the value of loopTill, and then it will be finished. And inside the loop, we’re adding the value
of i to the addNumbers variable. So, addition goes like 1+2+3+ till 20 which
is 210, as we’re getting here. You can use downTo keyword in for loop as
well, where loop will start from 20 till 0 Let’s see it practically. As you can see here, the loop starts from
20 all the way to 0, and again it gives us the same output. Continue statement is used when you want to
exclude a particular iteration from for loop. Continue statement skips the current cycle. This is how it is used: Let’s say you want to print even numbers
then with the use of “continue” keyword we can print them. Let’s see it practically. Here, we have successfully printed even numbers
from 1 to 20 break keyword breaks the loop completely and
stops from further running. Let’s see it practically: Here as you can see, when n comes to 4th iteration
it breaks, because of the condition mentioned here. when is a replacement for switch operator
of C-like languages. Similar to “switch” in other languages
you have “when” in Kotlin. Here’s how it is used: “when” matches its argument against all
branches sequentially until some branch condition is satisfied.
“when” can be used either as an expression or as a statement. when used as an expression
If it is used as an expression, the value of the satisfied branch becomes the value
of the overall expression. Make a note that, “If when is used as
an expression, the else branch is mandatory” when used as a statement
If it is used as a statement, the values of individual branches are ignored. First, we’ll practically see “when”
used as an expression: So, this is how we use “when as an expression”.
The else branch is evaluated if none of the other branch conditions are satisfied. Now, let’s see, “when” used as a statement Here, we’ve used when as a statement. You can also use, when expressions for data
types other than integers as well. Here’s an example As you can see here, we’ve used String as
a data type. So when we have BackToCoding as channel name,
it shows Subscribe to it immediately. Otherwise it shows Please subscribe to BackToCoding
as well. In the previous video, we saw if-else if-else
ladder, so we can use, when expressions for handling if-else ladder. Instead of having
multiple else clauses we can use advanced when expressions. Let us see them practically. Now, again we can simplify the existing code,
with the help of ranges. Which is definitely a better approach of writing
code. That’s it for this video, I hope you enjoyed
it. Please give it a like and comment down your thoughts below. Don’t forget to check my other videos which
can be helpful to you.

Leave a Reply

Your email address will not be published. Required fields are marked *