File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed
Tutorials/Basic/loops/for loops Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 01_for_loops.js
2
+ /*
3
+ This code snippet shows the usage of for loops in JavaScript.
4
+ The For loop is used to run a piece of code specified times
5
+ For Loops iterates through a block of code until the counter reaches a specified number
6
+ For loop consists of 3 statements ()
7
+ For loop is most used loop in JavaScript
8
+ */
9
+
10
+
11
+ /*
12
+ Basic Syntax
13
+
14
+ for (statement 1; statement 2; statement 3) {
15
+ // Code to be executed
16
+ }
17
+ */
18
+
19
+
20
+ /*
21
+ for(initialization; condition; final expression) {
22
+ // Code to be executed
23
+ }
24
+ */
25
+
26
+ for ( var i = 0 ; i < 20 ; i ++ ) {
27
+ console . log ( i )
28
+ }
29
+
30
+ // Finding odd numbers
31
+
32
+ let oddNumberArray1 = [ ]
33
+
34
+ for ( var i = 0 ; i < 20 ; i ++ ) {
35
+ if ( i % 2 != 0 ) {
36
+ oddNumberArray1 . push ( i )
37
+ }
38
+ }
39
+ console . log ( oddNumberArray1 )
40
+
41
+ // Alternate way
42
+
43
+ let oddNumberArray2 = [ ]
44
+ for ( var i = 1 ; i < 20 ; i += 2 ) {
45
+ oddNumberArray2 . push ( i )
46
+ }
47
+ console . log ( oddNumberArray2 )
48
+
49
+ // Finding even numbers
50
+
51
+ let evenNumberArray = [ ]
52
+
53
+ for ( var x = 0 ; x < 20 ; x += 2 ) {
54
+ evenNumberArray . push ( x )
55
+ }
56
+ console . log ( evenNumberArray )
57
+
58
+ // Counting backwards
59
+
60
+ let backwardCounting = [ ]
61
+
62
+ for ( var x = 20 ; x > 0 ; x -- ) {
63
+ backwardCounting . push ( x )
64
+ }
65
+ console . log ( backwardCounting )
You can’t perform that action at this time.
0 commit comments