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.21
List Price:  €68.43
You save:  €34.22
£29.66
List Price:  £59.33
You save:  £29.67
CA$55.16
List Price:  CA$110.35
You save:  CA$55.18
A$61.10
List Price:  A$122.22
You save:  A$61.12
S$51.46
List Price:  S$102.93
You save:  S$51.47
HK$312.03
List Price:  HK$624.14
You save:  HK$312.11
CHF 32.11
List Price:  CHF 64.24
You save:  CHF 32.12
NOK kr401.67
List Price:  NOK kr803.45
You save:  NOK kr401.77
DKK kr255.41
List Price:  DKK kr510.89
You save:  DKK kr255.47
NZ$68.04
List Price:  NZ$136.11
You save:  NZ$68.06
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,868.14
List Price:  ৳9,737.51
You save:  ৳4,869.36
₹3,529.50
List Price:  ₹7,059.88
You save:  ₹3,530.38
RM168.90
List Price:  RM337.86
You save:  RM168.95
₦61,122.31
List Price:  ₦122,259.91
You save:  ₦61,137.60
₨11,352.43
List Price:  ₨22,707.71
You save:  ₨11,355.27
฿1,287.39
List Price:  ฿2,575.11
You save:  ฿1,287.72
₺1,649.77
List Price:  ₺3,299.95
You save:  ₺1,650.18
B$217.85
List Price:  B$435.76
You save:  B$217.91
R707.27
List Price:  R1,414.71
You save:  R707.44
Лв66.98
List Price:  Лв133.98
You save:  Лв67
₩55,593.84
List Price:  ₩111,201.58
You save:  ₩55,607.74
₪133.56
List Price:  ₪267.16
You save:  ₪133.59
₱2,275.61
List Price:  ₱4,551.79
You save:  ₱2,276.18
¥5,925.49
List Price:  ¥11,852.46
You save:  ¥5,926.97
MX$746.88
List Price:  MX$1,493.95
You save:  MX$747.07
QR145.59
List Price:  QR291.22
You save:  QR145.63
P575.58
List Price:  P1,151.30
You save:  P575.72
KSh5,166.70
List Price:  KSh10,334.70
You save:  KSh5,168
E£1,941.52
List Price:  E£3,883.53
You save:  E£1,942
ብር5,721.79
List Price:  ብር11,445.02
You save:  ብር5,723.22
Kz36,469.08
List Price:  Kz72,947.28
You save:  Kz36,478.20
CLP$38,879.87
List Price:  CLP$77,769.47
You save:  CLP$38,889.60
CN¥285.16
List Price:  CN¥570.39
You save:  CN¥285.23
RD$2,535.76
List Price:  RD$5,072.16
You save:  RD$2,536.40
DA5,193.23
List Price:  DA10,387.77
You save:  DA5,194.53
FJ$90.23
List Price:  FJ$180.48
You save:  FJ$90.25
Q306.89
List Price:  Q613.87
You save:  Q306.97
GY$8,368.31
List Price:  GY$16,738.73
You save:  GY$8,370.41
ISK kr4,899.57
List Price:  ISK kr9,800.37
You save:  ISK kr4,900.80
DH362.96
List Price:  DH726.02
You save:  DH363.06
L670.43
List Price:  L1,341.04
You save:  L670.60
ден2,112.24
List Price:  ден4,225.01
You save:  ден2,112.77
MOP$321.36
List Price:  MOP$642.81
You save:  MOP$321.44
N$709.42
List Price:  N$1,419.02
You save:  N$709.60
C$1,471.90
List Price:  C$2,944.18
You save:  C$1,472.27
रु5,637.55
List Price:  रु11,276.52
You save:  रु5,638.96
S/140.86
List Price:  S/281.76
You save:  S/140.90
K169.50
List Price:  K339.04
You save:  K169.54
SAR150.04
List Price:  SAR300.13
You save:  SAR150.08
ZK951.86
List Price:  ZK1,903.96
You save:  ZK952.10
L173.78
List Price:  L347.61
You save:  L173.82
Kč835.99
List Price:  Kč1,672.19
You save:  Kč836.19
Ft13,444.53
List Price:  Ft26,892.42
You save:  Ft13,447.89
SEK kr376.32
List Price:  SEK kr752.74
You save:  SEK kr376.41
ARS$54,486.61
List Price:  ARS$108,986.84
You save:  ARS$54,500.23
Bs276.50
List Price:  Bs553.08
You save:  Bs276.57
COP$159,741.91
List Price:  COP$319,523.76
You save:  COP$159,781.85
₡20,221.55
List Price:  ₡40,448.16
You save:  ₡20,226.60
L1,047.97
List Price:  L2,096.21
You save:  L1,048.24
₲288,326.82
List Price:  ₲576,725.74
You save:  ₲288,398.92
$U1,602.41
List Price:  $U3,205.22
You save:  $U1,602.81
zł145.54
List Price:  zł291.13
You save:  zł145.58
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.