Working with the While Loop

Modern JavaScript Using Control Structures
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€96.21
List Price:  €134.70
You save:  €38.49
£79.93
List Price:  £111.90
You save:  £31.97
CA$143.79
List Price:  CA$201.32
You save:  CA$57.52
A$160.48
List Price:  A$224.69
You save:  A$64.20
S$135.83
List Price:  S$190.17
You save:  S$54.33
HK$777.07
List Price:  HK$1,087.93
You save:  HK$310.86
CHF 89.45
List Price:  CHF 125.24
You save:  CHF 35.78
NOK kr1,141.95
List Price:  NOK kr1,598.78
You save:  NOK kr456.82
DKK kr717.64
List Price:  DKK kr1,004.72
You save:  DKK kr287.08
NZ$177.52
List Price:  NZ$248.53
You save:  NZ$71.01
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,994.74
List Price:  ৳16,793.12
You save:  ৳4,798.37
₹8,496.63
List Price:  ₹11,895.62
You save:  ₹3,398.99
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦155,628.43
List Price:  ₦217,886.03
You save:  ₦62,257.60
₨27,934.96
List Price:  ₨39,110.06
You save:  ₨11,175.10
฿3,444.45
List Price:  ฿4,822.37
You save:  ฿1,377.92
₺3,518.54
List Price:  ₺4,926.10
You save:  ₺1,407.55
B$617.53
List Price:  B$864.57
You save:  B$247.04
R1,837.80
List Price:  R2,572.99
You save:  R735.19
Лв188.31
List Price:  Лв263.64
You save:  Лв75.33
₩144,872.25
List Price:  ₩202,826.95
You save:  ₩57,954.69
₪364.89
List Price:  ₪510.86
You save:  ₪145.97
₱5,883.01
List Price:  ₱8,236.45
You save:  ₱2,353.44
¥15,672.80
List Price:  ¥21,942.55
You save:  ¥6,269.75
MX$2,028.91
List Price:  MX$2,840.56
You save:  MX$811.64
QR366.34
List Price:  QR512.89
You save:  QR146.55
P1,382.59
List Price:  P1,935.69
You save:  P553.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,091.27
List Price:  E£7,127.98
You save:  E£2,036.71
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$99,155.08
List Price:  CLP$138,821.08
You save:  CLP$39,666
CN¥729.81
List Price:  CN¥1,021.77
You save:  CN¥291.95
RD$6,104.52
List Price:  RD$8,546.58
You save:  RD$2,442.05
DA13,454.60
List Price:  DA18,836.98
You save:  DA5,382.37
FJ$232.11
List Price:  FJ$324.96
You save:  FJ$92.85
Q773.21
List Price:  Q1,082.52
You save:  Q309.31
GY$21,000.46
List Price:  GY$29,401.48
You save:  GY$8,401.02
ISK kr13,903.60
List Price:  ISK kr19,465.60
You save:  ISK kr5,562
DH1,007.40
List Price:  DH1,410.40
You save:  DH403
L1,841.78
List Price:  L2,578.57
You save:  L736.78
ден5,922.50
List Price:  ден8,291.74
You save:  ден2,369.23
MOP$803.42
List Price:  MOP$1,124.82
You save:  MOP$321.40
N$1,834.70
List Price:  N$2,568.66
You save:  N$733.95
C$3,694.32
List Price:  C$5,172.20
You save:  C$1,477.87
रु13,665.58
List Price:  रु19,132.35
You save:  रु5,466.77
S/374.71
List Price:  S/524.60
You save:  S/149.89
K406.86
List Price:  K569.63
You save:  K162.76
SAR375.72
List Price:  SAR526.02
You save:  SAR150.30
ZK2,777.96
List Price:  ZK3,889.25
You save:  ZK1,111.29
L478.81
List Price:  L670.35
You save:  L191.54
Kč2,418.25
List Price:  Kč3,385.65
You save:  Kč967.40
Ft39,800.47
List Price:  Ft55,722.25
You save:  Ft15,921.78
SEK kr1,103.66
List Price:  SEK kr1,545.17
You save:  SEK kr441.50
ARS$102,190.76
List Price:  ARS$143,071.15
You save:  ARS$40,880.39
Bs693.61
List Price:  Bs971.08
You save:  Bs277.47
COP$437,799.12
List Price:  COP$612,936.28
You save:  COP$175,137.16
₡50,480.33
List Price:  ₡70,674.48
You save:  ₡20,194.15
L2,548.03
List Price:  L3,567.35
You save:  L1,019.31
₲784,126.06
List Price:  ₲1,097,807.85
You save:  ₲313,681.79
$U4,460.41
List Price:  $U6,244.75
You save:  $U1,784.34
zł409.96
List Price:  zł573.96
You save:  zł164
Already have an account? Log In

Transcript

Let's take a look at loops. Now remember the purpose of a loop is to cycle through JavaScript statements until a certain condition is met. The first type of loop that we will look at is the while loop. Let's take a look at the syntax for the while loop. First off, you must begin with the word while that is a reserved word. And then inside of parentheses you present the condition so very similar to the if conditional.

Then you have the block of code that will execute that can be one or more statements, and that code executes every iteration of the loop and finally, you close with a curly brace. So the two curly braces indicate the block of code which needs to execute. The code will continue to execute as long as the condition for the while loop evaluates to true as soon as it evaluates to false, it will exit out of the loop. Now there's another loop structure in JavaScript very similar to the while loop. It's not used very often, basically because it accomplishes the same thing that the while loop accomplishes. But I just want to show it to you before we do an example of the while loop.

That's the do while loop. And like I said, it works just like the while loop. The only difference is that the code block will execute at least once. So the condition is not checked until the code block has executed. So, it begins with the word do then inside of the curly braces is the block of code to execute. And then there is a while statement and a condition.

So the code blocks executes then it checks the condition if the condition is still true. It goes through and executes the code block again and continues to do that until it is false. So So the main difference between the do while and the while is that the do while will always execute the code block at least once the while not necessarily. Now you can accomplish the same thing with both types of loops. There's one more loop we'll look at in a subsequent movie, but these two are very similar the while in a do while so I presented them together. Alright, so back to the while loop syntax because this is the one we're going to do the exercise on.

Let's go ahead and open up sublime. Now what we want to accomplish with this exercise is we want to generate a random number between zero and 10. And we want to do it until we get a five and then we want to print out how many times it took to get to that number five. Okay, that's a good good situation where we would want to use a while loop. So let's first set up some variables. We want to set up a random num variable, this is going to contain our random number.

And then we also want to set up a counter. We'll use this counter to determine how many times it takes to get a five in our random number. And we need to initialize this variable to a zero because it's a counter for us. We want to start at zero. We've done it zero times. Alright, let's go ahead and create our while loop.

We begin with the keyword while then inside the parentheses. We put our conditional and we want that conditional be while the random number is not equal to five. Great and then we put our code the We put our curly braces to indicate our code block. Now first let's assign a number to random num, a random number two random. So random equals math dot random. And we want to multiply that by 10.

Because we want a random number between zero and 10. Okay. Now, as you may remember from the last time we did, we use random in the math object that it presents a decimal number between zero and one. When we multiply that by 10, there still is a number of digits after the decimal point. So how do we get rid of that? You may remember that we have to round it.

Now I could do a second line here. But instead I'm just going to put the results of math dot random multiplied by 10 inside math dot round, so open parentheses there, and then I put a close parentheses after the 10. That will round the results of math dot random times 10. Okay, and now one last thing we need to do is we need to increment our counter. Remember the increment operator plus plus, that will add one to it. That is exactly the same thing as counter equals counter plus one.

Okay, both of those would work. Obviously, counter plus plus is shorter to type. All right, now we simply need to print out our results. So let's do a console dot log. So basically, once ran num equals five, it will exit out of the loop, the loop will stop and then we can go ahead and print out our results. So while the loop is running, counter is incremented each time it goes through an iteration.

So counter will indicate how many times it went through that before it got to a five. So let's print that information out console dot log. And just for the sake of giving another example of template string literals, let's use that to print out the data. So I'm going to do a backtick it took, and then we'll put in the counter variable, because this is how many times it took to reach and now let's put in the random variable, we'll just use that variable because then we can make sure that random is set to five, if we want it to be set to five, okay. And then it will conclude that was one more backtick and the statement needs to be concluded with the semicolon. All right, let's take a look and see how this works.

So we're going to go ahead and save this. I'm going to grab the file path to my HTML file. And then we'll put that inside of the browser. Let's go ahead and open up the console. And this time, it said it took three times to reach five. All right, let me dock the console, so we can try to execute this couple more times it took three times reach five, if I refresh, we get it took 39 times to reach five.

Refresh again, took 19 times to reach five. Okay, I want to make one more change on our code here to make it a little more reusable. So right now, the code is set up to only do this if we want to find the number five How can we make this so that it could be any number we wanted between zero and 10? Well, we could set this up in a variable once again, another reason for a variable. So let's call this variable and value and let's set it equal to five. Whoops, then we can change and value anytime we want.

Now, obviously, in this small bit of code we have here it'd be just as easy to change in value as it would be to change this number. But if we were doing a large program, and we had our variables at the top, and who knows how many time we're using that variable, that would be a situation where you'd want to change the variable only and it would change it for the rest of the program. Just as an example, that we can put in value, and then we could also have n value in our console dot log statements, so then we use the variable twice. We only need to change it once up here. Okay, let's go ahead and save that. Jump out again.

Test it 20 times 52 times just kind of interesting sees one time. Wow. All right, so there's our first example of a while loop. We're going to do one more example in the next movie.

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.