Discussion with Diagram and Examples of While loop

7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.18
List Price:  €95.97
You save:  €28.79
£55.85
List Price:  £79.79
You save:  £23.94
CA$98.21
List Price:  CA$140.31
You save:  CA$42.09
A$107.62
List Price:  A$153.75
You save:  A$46.13
S$94.24
List Price:  S$134.64
You save:  S$40.39
HK$544.83
List Price:  HK$778.36
You save:  HK$233.53
CHF 62.54
List Price:  CHF 89.34
You save:  CHF 26.80
NOK kr774.97
List Price:  NOK kr1,107.14
You save:  NOK kr332.17
DKK kr501
List Price:  DKK kr715.75
You save:  DKK kr214.74
NZ$119.95
List Price:  NZ$171.37
You save:  NZ$51.41
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,361.58
List Price:  ৳11,945.63
You save:  ৳3,584.04
₹5,909.84
List Price:  ₹8,442.99
You save:  ₹2,533.15
RM312.71
List Price:  RM446.75
You save:  RM134.04
₦118,484.88
List Price:  ₦169,271.38
You save:  ₦50,786.49
₨19,443.22
List Price:  ₨27,777.22
You save:  ₨8,334
฿2,412.28
List Price:  ฿3,446.26
You save:  ฿1,033.98
₺2,418.32
List Price:  ₺3,454.90
You save:  ₺1,036.57
B$406.01
List Price:  B$580.04
You save:  B$174.03
R1,270.71
List Price:  R1,815.38
You save:  R544.66
Лв131.35
List Price:  Лв187.66
You save:  Лв56.30
₩98,301.65
List Price:  ₩140,436.95
You save:  ₩42,135.30
₪260.01
List Price:  ₪371.46
You save:  ₪111.45
₱4,125.14
List Price:  ₱5,893.31
You save:  ₱1,768.17
¥10,832.35
List Price:  ¥15,475.45
You save:  ¥4,643.10
MX$1,429.79
List Price:  MX$2,042.64
You save:  MX$612.85
QR255.18
List Price:  QR364.56
You save:  QR109.38
P956.90
List Price:  P1,367.06
You save:  P410.16
KSh9,061.51
List Price:  KSh12,945.58
You save:  KSh3,884.06
E£3,475.01
List Price:  E£4,964.52
You save:  E£1,489.50
ብር8,566
List Price:  ብር12,237.67
You save:  ብር3,671.66
Kz63,900.87
List Price:  Kz91,290.87
You save:  Kz27,390
CLP$68,444.99
List Price:  CLP$97,782.75
You save:  CLP$29,337.76
CN¥506.93
List Price:  CN¥724.22
You save:  CN¥217.29
RD$4,217.06
List Price:  RD$6,024.63
You save:  RD$1,807.57
DA9,397.90
List Price:  DA13,426.15
You save:  DA4,028.25
FJ$159.29
List Price:  FJ$227.57
You save:  FJ$68.27
Q540.13
List Price:  Q771.64
You save:  Q231.51
GY$14,638.82
List Price:  GY$20,913.50
You save:  GY$6,274.67
ISK kr9,773.54
List Price:  ISK kr13,962.80
You save:  ISK kr4,189.25
DH703.91
List Price:  DH1,005.63
You save:  DH301.72
L1,275.33
List Price:  L1,821.98
You save:  L546.64
ден4,132.76
List Price:  ден5,904.20
You save:  ден1,771.43
MOP$561.01
List Price:  MOP$801.48
You save:  MOP$240.46
N$1,268.91
List Price:  N$1,812.81
You save:  N$543.90
C$2,574.70
List Price:  C$3,678.31
You save:  C$1,103.60
रु9,449.77
List Price:  रु13,500.25
You save:  रु4,050.48
S/265.32
List Price:  S/379.05
You save:  S/113.72
K281.71
List Price:  K402.47
You save:  K120.75
SAR262.77
List Price:  SAR375.40
You save:  SAR112.63
ZK1,934.92
List Price:  ZK2,764.29
You save:  ZK829.37
L334.42
List Price:  L477.77
You save:  L143.34
Kč1,702.59
List Price:  Kč2,432.37
You save:  Kč729.78
Ft27,646.05
List Price:  Ft39,496.05
You save:  Ft11,850
SEK kr772.42
List Price:  SEK kr1,103.50
You save:  SEK kr331.08
ARS$70,251.41
List Price:  ARS$100,363.46
You save:  ARS$30,112.05
Bs483.50
List Price:  Bs690.75
You save:  Bs207.24
COP$307,238.59
List Price:  COP$438,931.09
You save:  COP$131,692.49
₡35,641.51
List Price:  ₡50,918.63
You save:  ₡15,277.11
L1,768.23
List Price:  L2,526.16
You save:  L757.92
₲546,248.87
List Price:  ₲780,388.98
You save:  ₲234,140.10
$U2,983.14
List Price:  $U4,261.82
You save:  $U1,278.67
zł291.40
List Price:  zł416.31
You save:  zł124.90
Already have an account? Log In

Transcript

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

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.