Principles of Computing-8.4
What are the first and last numbers output by the code segment? var c = 100; while (c > 0) { console.log(c); c -= 10; } 100 and 0. 90 and 0. 100 and 10.
100 and 10
The do-while loop
executes the loop body before checking the loop's condition to determine if the loop should execute again, repeating until the condition is false.
What is c when the loop terminates? var c = 10; while (c <= 20) console.log(c); c += 5; 15 20 The loop never terminates.
The loop never terminates
What condition makes the loop output the even numbers 2 through 20? var c = 2; while (_____) { console.log(c); c += 2; } c >= 20 c <= 20 c < 20
c<= 20
Which condition causes the for loop to output the numbers 100 down to 50, inclusively? for (c = 100; ______; c--) { console.log(c); } c < 50 c > 50 c >= 50
c>=50
the continue statement
causes loop to iterate again without executing the remaining statements in the loop
Loop body
is the statements that a loop repeatedly executes.
3 parts a loop collects
the loop variable initialization, loop condition, and loop variable update
What is the value of c when the loop terminates? var c = 10; while (c <= 20); { console.log(c); c += 5; } 25 20 The loop never terminates.
The loop never terminates
Infinite loop
a loop that never ends
While loop
a looping structure that checks if the loop's condition is true before executing the loop body, repeating until the condition is false
What numbers are output by the code segment? for (x = 1; x <= 3; x++) { for (y = 2; y <= 4; y++) { console.log(y); } } 2, 3, 4 2, 3, 4, 2, 3, 4, 2, 3, 4 2, 3, 4, 5, 6, 7, 8, 9, 10
2, 3, 4, 2, 3, 4, 2, 3, 4
What is the last number output to the console? var c = 10; do { console.log(c); c--; } while (c >= 5);
5
What is the last number output to the console? var x = 1; do { var y = 0; do { console.log(x + y); y++; } while (y < 3); x++; } while (x < 4);
5
What numbers are output by the code segment? for (c = 5; c < 10; c += 2) { console.log(c); } 5, 7, 9 5, 6, 7, 8, 9 Infinite loop
5,7,9
break statements
breaks out of a loop prematurely
Which loop always executes the loop body at least once? while do-while for
do-while
for-loop
executes the initialization expression, evaluates the loop's condition, and executes the loop body if the condition is true. After the loop body executes, the final expression is evaluated, and the loop condition is checked to determine if the loop should execute again.
Write a condition that executes the do-while loop as long as the user enters a negative number. var num; do { num = prompt("Enter a negative number:"); } while (______);
num < 0