File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
Tutorials/Basic/loops/while loops Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 01_while_loops.js
2
+ /*
3
+ This code snippet shows the usage of while loops in JavaScript.
4
+ The while loop loops (what ?) until the specified condition is met
5
+ */
6
+
7
+ // Syntax
8
+ /*
9
+ while (condition) {
10
+ code
11
+ }
12
+ */
13
+
14
+ // Initialize a variable to keep track of the number of days
15
+ let days = 1 ;
16
+
17
+ // Start the while loop
18
+ while ( days <= 5 ) {
19
+ console . log ( `Day ${ days } : Work hard, have fun!` ) ; // Print the message for each day
20
+ days ++ ; // Increment the number of days
21
+ }
22
+
23
+ let count = 0 ;
24
+
25
+ while ( count < 10 ) // JS has 0 based indexing i.e indexing starts from 0 so 10 would end at 9 only
26
+ {
27
+ console . log ( `Current count is ${ count } ` ) ;
28
+ count ++ ; // Increment operator
29
+ }
You can’t perform that action at this time.
0 commit comments