While Loops

3 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$39.99
List Price:  $79.99
You save:  $40
€34.36
List Price:  €68.73
You save:  €34.37
£29.84
List Price:  £59.69
You save:  £29.85
CA$55.08
List Price:  CA$110.18
You save:  CA$55.10
A$61.41
List Price:  A$122.85
You save:  A$61.43
S$51.52
List Price:  S$103.05
You save:  S$51.53
HK$312.13
List Price:  HK$624.35
You save:  HK$312.21
CHF 32.19
List Price:  CHF 64.39
You save:  CHF 32.20
NOK kr400.12
List Price:  NOK kr800.35
You save:  NOK kr400.22
DKK kr256.45
List Price:  DKK kr512.97
You save:  DKK kr256.51
NZ$68.27
List Price:  NZ$136.57
You save:  NZ$68.29
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,869.27
List Price:  ৳9,739.76
You save:  ৳4,870.49
₹3,524.71
List Price:  ₹7,050.30
You save:  ₹3,525.59
RM169.27
List Price:  RM338.59
You save:  RM169.32
₦61,275.07
List Price:  ₦122,565.47
You save:  ₦61,290.40
₨11,351.57
List Price:  ₨22,705.99
You save:  ₨11,354.41
฿1,295.43
List Price:  ฿2,591.19
You save:  ฿1,295.76
₺1,645.70
List Price:  ₺3,291.82
You save:  ₺1,646.11
B$217.74
List Price:  B$435.55
You save:  B$217.80
R708.73
List Price:  R1,417.64
You save:  R708.90
Лв67
List Price:  Лв134.02
You save:  Лв67.01
₩55,770.52
List Price:  ₩111,554.98
You save:  ₩55,784.46
₪135.60
List Price:  ₪271.25
You save:  ₪135.64
₱2,298.32
List Price:  ₱4,597.22
You save:  ₱2,298.90
¥5,941.06
List Price:  ¥11,883.61
You save:  ¥5,942.55
MX$750.26
List Price:  MX$1,500.70
You save:  MX$750.44
QR146.03
List Price:  QR292.09
You save:  QR146.06
P574.53
List Price:  P1,149.20
You save:  P574.67
KSh5,166.70
List Price:  KSh10,334.70
You save:  KSh5,168
E£1,939.49
List Price:  E£3,879.47
You save:  E£1,939.98
ብር5,681.79
List Price:  ብር11,365.01
You save:  ብር5,683.21
Kz36,469.08
List Price:  Kz72,947.28
You save:  Kz36,478.20
CLP$38,717.51
List Price:  CLP$77,444.71
You save:  CLP$38,727.20
CN¥285.16
List Price:  CN¥570.39
You save:  CN¥285.23
RD$2,516.45
List Price:  RD$5,033.54
You save:  RD$2,517.08
DA5,193.10
List Price:  DA10,387.51
You save:  DA5,194.40
FJ$90.45
List Price:  FJ$180.93
You save:  FJ$90.48
Q306.92
List Price:  Q613.92
You save:  Q307
GY$8,374.04
List Price:  GY$16,750.18
You save:  GY$8,376.14
ISK kr4,933.96
List Price:  ISK kr9,869.16
You save:  ISK kr4,935.20
DH359.99
List Price:  DH720.08
You save:  DH360.08
L664.67
List Price:  L1,329.51
You save:  L664.84
ден2,102.60
List Price:  ден4,205.72
You save:  ден2,103.12
MOP$321.59
List Price:  MOP$643.27
You save:  MOP$321.67
N$703.83
List Price:  N$1,407.84
You save:  N$704.01
C$1,473.65
List Price:  C$2,947.67
You save:  C$1,474.01
रु5,648.69
List Price:  रु11,298.79
You save:  रु5,650.10
S/141.43
List Price:  S/282.89
You save:  S/141.46
K169.54
List Price:  K339.12
You save:  K169.58
SAR150.05
List Price:  SAR300.14
You save:  SAR150.09
ZK946.07
List Price:  ZK1,892.38
You save:  ZK946.31
L174.54
List Price:  L349.12
You save:  L174.58
Kč841.12
List Price:  Kč1,682.46
You save:  Kč841.33
Ft13,613.08
List Price:  Ft27,229.58
You save:  Ft13,616.49
SEK kr378.09
List Price:  SEK kr756.29
You save:  SEK kr378.19
ARS$55,055.52
List Price:  ARS$110,124.81
You save:  ARS$55,069.29
Bs276.68
List Price:  Bs553.44
You save:  Bs276.75
COP$161,780.26
List Price:  COP$323,600.99
You save:  COP$161,820.72
₡20,244.43
List Price:  ₡40,493.93
You save:  ₡20,249.49
L1,048.40
List Price:  L2,097.07
You save:  L1,048.66
₲289,232.23
List Price:  ₲578,536.79
You save:  ₲289,304.56
$U1,600.17
List Price:  $U3,200.74
You save:  $U1,600.57
zł146.62
List Price:  zł293.27
You save:  zł146.65
Already have an account? Log In

Transcript

The other option of repeating a specific action many times is by using while loops. And to create a while loop, what you would do is you would just type while and then parentheses and inside them, you add your variable. But the variable here is no is not going to be declared as we did with for loops. But in for in my loops we need to add, we need to declare that variable before the loop. So here I'm going to add lit i equals zero. So we did we first declare the variable that is going to be increased many times.

And then here inside it, we add I and then smaller than and then the condition. This is the condition and then for example 10 because we want it to be repeated In this code, we want it to be repeated 10 times and then we add curly braces and inside those curly braces, we add our code that we want it to be repeated as long as this condition is met. So, this condition is that I is smaller than 10. And once i is greater than 10, we will get out of this loop. But remember, here we did not add a way of increasing I in for loops, the way of increasing guy is has been added inside before loop parentheses by adding i plus plus, but in while loops in order to increase I, at the end of the loop and before the closing bracket, we add i plus plus, which means that the code here will first be implemented and then I will be increased and then it is going to go all the way again to The beginning of the loop and checks if I is still smaller than 10.

If I is still smaller than 10, it is going to repeat the same code. So here is the code that we want to repeat code to repeat, which is console dot log. And then hi john, I, john, and then semicolon. And now if I save and run our program, as you can see, I get hi john 10 times 1-234-567-8910. So we get john 10 times by using a very simple code by using wild loops. So the difference between while and for is that in for loops, we have the variable declared inside the parentheses of the form of the for loop and I is going to be increased inside the parentheses of the for loop.

But in while loops, we add we declare first our integer before the loop itself and the condition is inside. But I is going to be increased from inside the, the while loop. So to recap, while loops are a great way of repeating the same code over and over again, without the need of repeating ourselves many many times and wasting our time

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.