13.2 While and Do-While Loops (15 mins)
while (condition) { // code to run while the condition is true }let i = 1; while (i <= 5) { console.log(i); // Outputs: 1, 2, 3, 4, 5 i++; }
do { // code to run at least once } while (condition);let i = 1; do { console.log(i); // Outputs: 1, 2, 3, 4, 5 i++; } while (i <= 5);
let x = 10; while (x < 5) { console.log(x); // This will not run because the condition is false }let x = 10; do { console.log(x); // Outputs: 10 (runs once, even though the condition is false) } while (x < 5);
Student Activity (15 mins):
Last updated