For 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.31
List Price:  €68.64
You save:  €34.32
£29.96
List Price:  £59.93
You save:  £29.96
CA$55.34
List Price:  CA$110.70
You save:  CA$55.35
A$60.18
List Price:  A$120.37
You save:  A$60.19
S$51.84
List Price:  S$103.70
You save:  S$51.85
HK$311.15
List Price:  HK$622.37
You save:  HK$311.22
CHF 32.21
List Price:  CHF 64.43
You save:  CHF 32.22
NOK kr404.15
List Price:  NOK kr808.40
You save:  NOK kr404.25
DKK kr256.32
List Price:  DKK kr512.71
You save:  DKK kr256.39
NZ$69.03
List Price:  NZ$138.08
You save:  NZ$69.04
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,888.84
List Price:  ৳9,778.90
You save:  ৳4,890.06
₹3,594.10
List Price:  ₹7,189.11
You save:  ₹3,595
RM164.55
List Price:  RM329.15
You save:  RM164.60
₦58,069.87
List Price:  ₦116,154.27
You save:  ₦58,084.40
₨11,221.19
List Price:  ₨22,445.19
You save:  ₨11,224
฿1,272.48
List Price:  ฿2,545.28
You save:  ฿1,272.80
₺1,702.30
List Price:  ₺3,405.03
You save:  ₺1,702.72
B$217.55
List Price:  B$435.16
You save:  B$217.61
R680.65
List Price:  R1,361.47
You save:  R680.82
Лв67.16
List Price:  Лв134.34
You save:  Лв67.18
₩58,678.52
List Price:  ₩117,371.72
You save:  ₩58,693.19
₪128.44
List Price:  ₪256.92
You save:  ₪128.48
₱2,379.03
List Price:  ₱4,758.66
You save:  ₱2,379.62
¥6,242.05
List Price:  ¥12,485.67
You save:  ¥6,243.61
MX$729.80
List Price:  MX$1,459.80
You save:  MX$729.99
QR145.60
List Price:  QR291.24
You save:  QR145.64
P532.16
List Price:  P1,064.45
You save:  P532.29
KSh5,168.70
List Price:  KSh10,338.70
You save:  KSh5,170
E£1,904.92
List Price:  E£3,810.32
You save:  E£1,905.40
ብር6,218.32
List Price:  ብር12,438.20
You save:  ብር6,219.87
Kz36,476.64
List Price:  Kz72,962.41
You save:  Kz36,485.76
CLP$36,905.97
List Price:  CLP$73,821.17
You save:  CLP$36,915.20
CN¥282.49
List Price:  CN¥565.05
You save:  CN¥282.56
RD$2,564.50
List Price:  RD$5,129.65
You save:  RD$2,565.14
DA5,201.70
List Price:  DA10,404.71
You save:  DA5,203
FJ$90.84
List Price:  FJ$181.72
You save:  FJ$90.87
Q306.33
List Price:  Q612.74
You save:  Q306.41
GY$8,366.87
List Price:  GY$16,735.84
You save:  GY$8,368.96
ISK kr5,106.32
List Price:  ISK kr10,213.92
You save:  ISK kr5,107.60
DH369.43
List Price:  DH738.96
You save:  DH369.52
L679.06
List Price:  L1,358.30
You save:  L679.23
ден2,116.43
List Price:  ден4,233.40
You save:  ден2,116.96
MOP$320.57
List Price:  MOP$641.22
You save:  MOP$320.65
N$679.24
List Price:  N$1,358.65
You save:  N$679.41
C$1,471.95
List Price:  C$2,944.27
You save:  C$1,472.32
रु5,761.08
List Price:  रु11,523.61
You save:  रु5,762.52
S/134.48
List Price:  S/269
You save:  S/134.52
K169.75
List Price:  K339.55
You save:  K169.79
SAR150.07
List Price:  SAR300.18
You save:  SAR150.11
ZK925.03
List Price:  ZK1,850.29
You save:  ZK925.26
L174.65
List Price:  L349.35
You save:  L174.70
Kč832.70
List Price:  Kč1,665.61
You save:  Kč832.91
Ft13,159.27
List Price:  Ft26,321.83
You save:  Ft13,162.56
SEK kr373.57
List Price:  SEK kr747.23
You save:  SEK kr373.66
ARS$57,365.65
List Price:  ARS$114,745.66
You save:  ARS$57,380
Bs277.32
List Price:  Bs554.72
You save:  Bs277.39
COP$153,328.58
List Price:  COP$306,695.51
You save:  COP$153,366.92
₡19,533.34
List Price:  ₡39,071.58
You save:  ₡19,538.23
L1,050.53
List Price:  L2,101.33
You save:  L1,050.80
₲275,091.83
List Price:  ₲550,252.46
You save:  ₲275,160.62
$U1,562.66
List Price:  $U3,125.72
You save:  $U1,563.05
zł145.28
List Price:  zł290.61
You save:  zł145.32
Already have an account? Log In

Transcript

So suppose that you want to agree john 10 times, would you write console dot log? Hi, john 10 times, that would be definitely a waste of time. So instead of writing the code 10 times by ourselves, we are going to use four loops. And to use a for loop, what you would do is you would just type four, and then you add parentheses, and inside them, you just start by creating an integer that starts by zero. So for example, I'm going to add lit i equals zero, and always in for loop. It's conventional to use AI.

As a variable. You can of course, use any other variable name like x, y, z or anything but it's conventional to use I venue at semi colon and then here you add something called condition. And what condition means is the number of times that you want to Print to print that action or to do that action. And as long as that condition is met, we are going to stay in the loop and do the same over and over again. And once once that condition is violated, we are going to get out of the loop. And in order to do our condition, I am going to use a very simple condition by using I and then smaller than 10.

And what that means is that as long as I is smaller than 10, we want to do the same code that is inside this for loop. And then we want to increase it so here I'm going to add another semicolon and then i plus plus and remember, plus plus means that we are going to increase I by one each single time. So AI is going to start by zero. And then it's going to go all the way to 10. And each time it increases, it is going to do and implement the code that is inside the for loop. And here I'm going to add, and then inside that loop, you should add the code that you want it to be implemented, as long as this condition is met.

So here what I'm going to do, I'm going to just use the console dot log, and then I'm going to add hi john inside it. Hi, john. And semicolon. Now if I save and run our program, as you can see here on the console, we have 1-234-567-8910. We have a hydrant 10 times printed on our screen 10 times instead of just writing this code 10 times We can just write as you can see 123 lines of code that has generated this output instead of writing the same code over and over and over again. And by the way, that will be very, very useful when you are dealing with a much complicated code in instead of writing the same code over and over again, you just need to use for loop and the job is done.

So to recap, for loops are used to repeat a specific code many times as long as the condition is met.

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.