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.51
List Price:  €69.04
You save:  €34.52
£30.03
List Price:  £60.08
You save:  £30.04
CA$56.07
List Price:  CA$112.15
You save:  CA$56.08
A$61.64
List Price:  A$123.30
You save:  A$61.66
S$51.95
List Price:  S$103.92
You save:  S$51.96
HK$310.72
List Price:  HK$621.52
You save:  HK$310.80
CHF 31.86
List Price:  CHF 63.74
You save:  CHF 31.87
NOK kr401.91
List Price:  NOK kr803.93
You save:  NOK kr402.01
DKK kr257.81
List Price:  DKK kr515.68
You save:  DKK kr257.87
NZ$69.66
List Price:  NZ$139.35
You save:  NZ$69.68
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,885.14
List Price:  ৳9,771.51
You save:  ৳4,886.36
₹3,508.98
List Price:  ₹7,018.84
You save:  ₹3,509.86
RM169.15
List Price:  RM338.35
You save:  RM169.20
₦58,662.13
List Price:  ₦117,338.93
You save:  ₦58,676.80
₨11,327.80
List Price:  ₨22,658.43
You save:  ₨11,330.63
฿1,315.63
List Price:  ฿2,631.59
You save:  ฿1,315.96
₺1,678.63
List Price:  ₺3,357.69
You save:  ₺1,679.05
B$215.47
List Price:  B$430.99
You save:  B$215.52
R698.96
List Price:  R1,398.11
You save:  R699.14
Лв67.44
List Price:  Лв134.89
You save:  Лв67.45
₩57,256.54
List Price:  ₩114,527.40
You save:  ₩57,270.86
₪131.86
List Price:  ₪263.76
You save:  ₪131.89
₱2,337.73
List Price:  ₱4,676.05
You save:  ₱2,338.31
¥6,071.28
List Price:  ¥12,144.08
You save:  ¥6,072.80
MX$735.85
List Price:  MX$1,471.89
You save:  MX$736.04
QR145.92
List Price:  QR291.87
You save:  QR145.95
P534.01
List Price:  P1,068.15
You save:  P534.14
KSh5,164.70
List Price:  KSh10,330.70
You save:  KSh5,166
E£1,901.52
List Price:  E£3,803.52
You save:  E£1,902
ብር6,038.47
List Price:  ብር12,078.45
You save:  ብር6,039.98
Kz36,470
List Price:  Kz72,949.12
You save:  Kz36,479.12
CLP$37,975.70
List Price:  CLP$75,960.90
You save:  CLP$37,985.20
CN¥284.92
List Price:  CN¥569.92
You save:  CN¥285
RD$2,547.56
List Price:  RD$5,095.76
You save:  RD$2,548.20
DA5,206.89
List Price:  DA10,415.09
You save:  DA5,208.20
FJ$91.93
List Price:  FJ$183.88
You save:  FJ$91.95
Q306.63
List Price:  Q613.34
You save:  Q306.71
GY$8,375.76
List Price:  GY$16,753.62
You save:  GY$8,377.85
ISK kr4,894.77
List Price:  ISK kr9,790.77
You save:  ISK kr4,896
DH368.81
List Price:  DH737.71
You save:  DH368.90
L679.76
List Price:  L1,359.69
You save:  L679.93
ден2,126.49
List Price:  ден4,253.52
You save:  ден2,127.02
MOP$320.46
List Price:  MOP$641
You save:  MOP$320.54
N$693.29
List Price:  N$1,386.76
You save:  N$693.46
C$1,473.12
List Price:  C$2,946.62
You save:  C$1,473.49
रु5,632.10
List Price:  रु11,265.62
You save:  रु5,633.51
S/135.05
List Price:  S/270.14
You save:  S/135.08
K170.84
List Price:  K341.73
You save:  K170.89
SAR149.97
List Price:  SAR299.99
You save:  SAR150.01
ZK905.77
List Price:  ZK1,811.78
You save:  ZK906
L175.43
List Price:  L350.90
You save:  L175.47
Kč839.13
List Price:  Kč1,678.47
You save:  Kč839.34
Ft13,432.80
List Price:  Ft26,868.96
You save:  Ft13,436.16
SEK kr377.49
List Price:  SEK kr755.09
You save:  SEK kr377.59
ARS$58,276.64
List Price:  ARS$116,567.85
You save:  ARS$58,291.21
Bs276.63
List Price:  Bs553.33
You save:  Bs276.69
COP$155,770.71
List Price:  COP$311,580.38
You save:  COP$155,809.66
₡20,093.12
List Price:  ₡40,191.28
You save:  ₡20,098.15
L1,051.86
List Price:  L2,103.99
You save:  L1,052.12
₲283,589.36
List Price:  ₲567,249.64
You save:  ₲283,660.27
$U1,592.21
List Price:  $U3,184.83
You save:  $U1,592.61
zł146.09
List Price:  zł292.22
You save:  zł146.12
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.