 # 4. Basic control flow in Kotlin | Kotlin Beginners Course

Hello, welcome to BackToCoding. Today, we’ll be talking about the “Kotlin’s
Basic 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 several different
datatypes in Kotlin. In this video, we’ll see Basic control flow. Control flow is nothing but, making a choice
when you have more than one scenarios and based on certain condition a path is chosen. For eg. While closing an application, there
are two choices given to the user, whether to close an application or not. And based
on the user’s choice the app has to decide what to do next. i.e. The flow of the program
is controlled by the control flow statements. And to understand control flow, you need to
have an understanding of Comparison operators. Comparison operators as the name suggests
let’s you compare the values. And to undertand comparison operators, you
need to know boolean operator’s and boolean logic. So, Booleans are commonly used to compare
2 or more values. Like, for eg. Whether two or more values are
equal to or not equal to and so on. Here’s a list of operators and their explanations Let’s see them practically. As you can see here,
In the first expression we are comparing two values and since 1 is not equal to 10, the
result is false. In the second expression, we’ve, “not
equal to” operator and as expected we’re getting the result as true.
Then, we have greater than operator where we are checking if 1 is greater than 10 which
is not, so we’re getting false. Similary, we’ve “less than” operator,
“Greater than or equal to” operator and “less than or equal to” operator. Now, let’s see what is Boolean logic. Boolean was invented by George Boole. Boolean
logic is a form of algebra in which all values are reduced to either TRUE or FALSE. Boolean
logic is especially important for computer science because it fits nicely with the binary
numbering system, in which each bit has a value of either 1 or 0. Here are some of them: AND logic says that, “if both of the conditions
are TRUE then the output is TRUE otherwise, it’s FALSE”. It means,
FALSE and FALSE gives you FALSE FALSE and TRUE gives you FALSE
TRUE and FALSE gives you FALSE TRUE and TRUE gives you TRUE When it comes to OR logic, it’s, “if both
are false then false otherwise it’s true” It means,
FALSE and FALSE gives you FALSE FALSE and TRUE gives you TRUE
TRUE and FALSE gives you TRUE TRUE and TRUE gives you TRUE And “not logic”, is just the “inverse
of the current condition” That means,
Inverse of True is False And for False it’s True Let’s see them practically, First, let’s see for AND condition
Second, we check for OR condition Third, we check for NOT condition Now, let’s move to our next topic, i.e.
if expression The if expression is the very basic form of
expression to control the flow of a program. It goes like, if a condition is met then only
perform certain things. For eg. Here, It will print “1 is less than 10” Whereas, if a condition is like this Then, it won’t print anything, since the
condition is not satisfied. You can also use if-else expression, where
if a condition is not satisfied in the “if block” then it will definitely go to the
“else block”. Let’s understand it with an another example. In this example, there are two variables,
myCondition and myCondition2, here myCondition say’s that I’ve subscribed to BackToCoding,
whereas myCondition2 says that I’ve not subscribed, which is really bad for our community,
right?. So, When myCondition is “I’ve subscribed
to BackToCoding” then you get the output as “Yes, you’re a subscriber”, which
we’re getting here And If I switch from myCondition to myCondition2
then the output will be “What are you waiting for?” Here, as you can see I’ve to switch the
conditions i.e. from myCondition to myCondition2. But it can again be handled in a new way,
which is using If-else if-else ladder. Let’s quickly make some changes to the code Here, in this program, as you can see,
We can handle a case which is unexpected. i.e. here we were expecting either a user
is a subscriber or not a subscriber but we’ve written a third condition which is completely
different. So this is how can handle it. Next we’ll see what are Loops Loops are something with the help of which
you can execute a set of code multiple times. So, let’s say you’ve a list of names,
and you have to find out a particular name from that list, so in such scenarios you can
use any type of loop. Let’s see them one by one. While loop:
A while loop is used to repeat a block of code several times. It is executed until a
condition is met. Here’s a syntax for While loop: Let’s see a practical example: Let’s say we want to print numbers upto
10, so what we’ll do is use println() function to print numbers upto 10 This is how a novice person will code. Which
was pretty easy right. But what if the requirement changes, which
happens a lot in software development , i.e. instead of printing numbers upto 10 I want
them to be printed upto 1000 , then what would you do. You can’t just write println 1000
times because it’s like doing donkey work, right.
So here comes our loops concept. So instead of writing println 1000 times,
we can use a while loop to print them As you can see, we’ve successfully printed
numbers upto 1000. Here the condition number++ is pretty important, otherwise it goes into
infinite loop. Next we’re going to print a table of 2,
which for a beginner would be really fascinating. Here, we’ve successfully printed the table
for 2 A version of while loop is do-while loop,
where a block of code is executed at least once without checking the condition in do
block, and then the condition is checked in while block.
So, here first the block of statements are executed and then the condition is checked. Here’s a syntax for do-while loop: Let’s print the table of 2 using do while 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