Well in this session we'll learn about different types of flow control
like if, el if, for, while etc that we are going to use in Python
next is the Python flow control
well these flow controls are the one which control the flow of execution of
your program so we have six different types of flow control in Python: if else,
nested if-else, for, while, break and continue. Let's discuss them one by one starting
with if-else. So what is this if else
let's see its syntax if the condition is true then execute
statement one else execute statement two. It's as simple as that let's see the flow
of execution. You started, you checked the condition if the condition is true then
execute block one if the condition is false then go to the else part and
execute block two. This block one it comes under F
part alright? Let's move on. Next is nested if-else let's see the
syntax if condition1 is true then execute
statement one else if condition 2 is true then execute statement true if none of
them is true then executes statement 3 let's check the flow control of it start
check for condition 1 that is if condition 1 is true so if condition 1 is
true then the statement executed with the if block is executed that is execute
block 1. If first condition is false then go and check for the second condition
that is else if condition. Now if else if condition is true then your second block
would be executed if none of them are true then finally the else block would
be executed that is execute block 3 and yeah remember one thing there is no
condition associated with a else block else block would only execute if all the
above conditions are false fine alright. Next is the for. Let's see the syntax
for iterating variable in sequence : execute statement check
well it's the example of a program like start we are defining a variable as
count equals zero then executes statement then increment the value of
count by one and then check the condition if count is less than 10. Is 1
less than 10? Yeah the condition is true so again repeat it and again execute the
statement again increment the value of count by one that is count become 2 and
again check if 2 is less than 3 and so on continue this till 10 is less than 10
and when this thing arrived that 10 is less than 10 your condition become false
and you exit the loop so this is how a for loop works. Let's understand this in a
better way with an example so there's our code all right we are defining list
fruits as apple, banana and cherry for X in fruits. X in fruit of 0s Apple then
print X. X's what? Apple. so Apple would be printed as the output. Now again
what we will do, will again go back and check for X in fruits. Now X of 1 is
banana then Print X. X is what banana so it will
print as banana again it will go back X and fruit of two is what? cherry
print X cherry would be printed
now if it goes back there is nothing left in the fruit so nothing would be
printed alright so you got the final output as apple banana and cherry
fine
so let's move ahead next is the while. The
syntax for this is while condition is true then execute set of statement under
the while
start check for the condition if it is true then execute block one and then
again repeat the loop until the condition is true. In case the condition
is false then just exit the loop. Let's understand this better with an example
here is my code like a equal one and variable part I got a equal one while a is
less than five. Is a equal one less than five? yeah condition is true and what
print the value of a
all right so we got the output as 1 after getting the output increase the
value of a by 2 so a plus equal to 2 that is 1 plus 2 equal 3. Now again go
back to the loop check for the condition. Is 3 less than 5
condition is true then again go to the print a part what
is a three so print a so you will get the output as 3. Again increment the
value of 8 to 2 that is a equal 5 now check the condition. Is 5 less than 5 no?
I think here we got the condition as false
so we got the final output us one and three all right? let's move ahead
here's another example, a equal 1 in the variable part a equal one check the
condition while a is less than three. Is one less than three? yeah condition is
true. Inside the while part the first condition that we check if a mod two
equal equal zero. Is one mod two equal equal zero? no the condition is false so
I will jump to the else part. So we'll jump to the else part and print a is odd
now since the value of a currently is one so one is odd fine? Then what we'll
do? We'll increment the value of a by one
that is one plus one equal two. Now again we'll go back to the while part now
we'll check if 2 is less than 3 yeah the condition is true fine now what
we'll do we'll jump to the if part and check if two mod two equal equal zero
yeah the condition is true so this time we'll check if two mod two equal equal
zero so yeah the condition is true so what it will do? It will print the if
part statement that is a is even. A in this case was 2 so 2 is even fine this is our
output. Next again it will go and increment the value of a by 1 so now two
becomes three again it will go back to the while part is 3 less than 3? the
condition is false
so your final output is one is odd and two is even fine so this was about the
while statement. let's move ahead and learn about break so this break
statement, it is used to break the loop at a certain condition all right
let's understand this with the help of this flow chart started check the
condition, if the condition is true then the break then check the break condition
if the break condition is false then only execute block one and repeat the
loop if the break condition comes to be true then break the entire loop and then
exit the loop all right let's check it with an example up here
for example equal 10
while is greater than zero
check for if he is
not equal to five if it is not equal to five
then
print
else
break it fine that's executive
say
this bench
a
- -
you
a - equal to one
stop it
you
just mention it up here
you
so we got the output as nine eight seven and six fine
so the moment loop becomes five we got exited out of the loop next we have is
the continue statement well the continue statement won't break
the loop it will just keep the statement in case the condition is true let's
understand this with the help of this flow chart so start check for condition
one if it has true then check for continued condition if it has false then
execute block and repeat then in case if the condition is true then stop
executing that particular block for that particular loop and cut for that
particular iteration and again go back and repeat and check the condition so
this was all for this session in case you have any doubt so you can reach out
to in telepath 24/7 and we'll try to resolve your query ASAP thank you