Hello, and welcome back. This is our third video of the series flow control in Python. By we have chosen Python is because of its popularity among young developers, and passionate and young data scientists. In this video we will be discussing Python by loop. The while loop in Python is used to iterate over a block of code as long as the test expression is true. Generally, it is used when we don't know beforehand when the condition is going to be false.
But we know within the system that at some point that condition is going to turn false. And the code will move out of that while loop syntax of while loop is while test expression is going to test the expression whether it's true or false. When it is true, it is going to run this code within the body of while loop and if it is false, it is going to come out of it. Let's see this in the form of diagram. The code enters the while loop, it tests the expression if it is true, it runs the body of y and it continues doing so as long as the condition is true. However, when the condition turns false, it exits out and while exiting, and there could be else block as well.
In this video we'll be discussing this in the form of two, two programs. The first program is printing out inside to and inside else. The else block is when the while loop, the condition becomes false and the end the program comes out of the while loop. And then this print inside else's printed out. So here we started the program using the counter variable and we assigned it the value of zero and this is zero and the condition is while the counter is less than three, it should print inside and then we are increasing the counter by One in the next line of code. So this iteration is going to continue for three times the first one, when the counter is zero it is going to print inside loop, then the counter at a value of one into it, so it becomes one.
Now again It tests inside lobe is printed, then it becomes two. And then in test the end, it is still less than three. So the entire room is printed out when it becomes three. So this condition turns false and then it prints out inside. Let's run this again. The inside loop is printed three times and then inside our second program is the fiber Nike sequence up to ns term.
This program is also very simple, but it has some conditions tested before we run the while loop. Here, we define the n terms. That is the number of terms that sequence should take in. It could be hard coded, like Here it is hard coded is 10, or it could be taken from the user as input. However, I've taken it as hard coded for the time, the first time is n one is zero, and two is the one and the counter that starts from zero. The logic is, if the user provides an input data is zero, that sequence is not going to like have many numbers, it will have only one number, and that is zero itself.
And in that case, the system could print out the message that please enter the positive in future. So or if the if the user has asked for a negative number or inputted a negative number, in that case, the system could print out, please enter positive in an integer. So we are checking the Enter if it is less than or equal to zero. We are asking the system to print a positive integer message. However, we know that we have already given it a number 10. So then this will not be the case to the court will not enter this condition will be false and this will Not entered this part of the code.
The next is a checking if the number is one because in that case only that would be printed. And now, this double equals to sign is that is used for comparative or comparison with the N term is equal to one or not. Here, we know that n is not equal to one. However, if the user is provided one, in that case, the condition would have been true, and the system would have been by a key sequence up to one column, and then it should have printed one printed zero because, and in that series, only zero would be there. The third part is when the user has given a number that is greater than one. In that case, first of all, we're going to print out the heading that is my next sequence up to and terms and terms is the variable where we store the value provided.
By the user if you give me a 10 to 10, and then a colon, so now we're going to check, as long as the number is through counter, that the Enter is, or the count is less than enter, because up to that point, the while loop should continue. So here, we're going to print n one, n one, zero initially, then n equals two and a comma sign. So in that case, when the zero is printed, have a comma after it, then we're going to increase the number the nth term and that term would be we'll have n one that is zero, and n two that is one added to it. And the first implication, it will be zero plus one, that would be one, then we're going to increase n one to N two by assigning the value of n two to N one. So it will have one them and the N two will pick up anything that already has is one so n two will become actually one again, because it was initially one and then the NFL was also wants It will become one, the counter will increase.
So what is the actual logic behind it is we're actually printing n one. So n one should be increasing by it's summing up the previous two numbers and that we are doing in the form of giving and when the value of n to n n to the value of n, and that is the sum of the previous two terms that we have already predefined in the program. So in the second iteration now, and that has the value of one and one has a value of one here it got it from and two, and two has a value of one. It was initially one. So one plus one it would have become two and one equals two and two and one was initially one and two was also one. So and one would remain one and two would be two, here it just updated and the value of two, so one plus two, that would be three.
So here then there there would be three printed. So this iteration would continue as long as this counter is less than the n terms and terms are defined But the user step. So when we run this program, it is going to print by Bernacchi sequence up to 10 numbers. Now you can reduce this to six. Let's see this. So it ran up to 501 sum of pasture number zero and there is nothing so it would remain one.
Then the sum of pasture number one plus one, two, then two plus one, three, then three plus two, five, and so on. I hope you have a better understanding of why loop and flow controls within Python you're enjoying these videos. May you have any questions feel free to contact me to more than I would be more than happy to answer. Thank you