Skip to content

Commit ee68fed

Browse files
committed
[Add] While Loops introduction
1 parent 73cffb1 commit ee68fed

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

0 commit comments

Comments
 (0)