Information Technology IT – 21 Programming for the web | e-Consult
21 Programming for the web (1 questions)
Login to see all questions.
Click on a question to view the answer
let sum = 0;
let i = 1;
do {
sum += i;
i++;
} while (i
Explanation:
- The
do-whileloop first executes the code block and then checks the condition. - The code block adds the current value of
ito thesumvariable. - The
ivariable is then incremented. - The loop continues as long as
iis less than or equal to 5. - Finally, the calculated
sumis displayed.
The do-while loop is suitable when you need to ensure that the loop body is executed at least once.