ISDS3105 Ch6.5 Loops

Ace your homework & exams now with Quizwiz!

i = 1; while (i <= 4) { console.log(i); i++; } console.log("Done!");

1. Assign i with 1. 2. 1 <= 4 is true, so the loop's body executes. 3. Output i to the console. 4. Increment i. 5. End of loop so go back to the top and re-evaluate the condition. 6. Keep executing loop until i <= 4 is false.

i = 1; do { console.log(i); i++; } while (i <= 4); console.log("Done!");

1. Assign i with 1. 2. do..while loop executes the loop body, evaluating the loop condition after the first execution. 3. The loop repeatedly executes until i <= 4 is false.

Which loop always executes the loop body at least once?

do-while The loop body is executed once before the loop condition is first evaluated.

For Loop for (initialization; condition; finalExpression) { body }

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.

Do-while loop do { body } while (condition);

executes the loop body before checking the loop's condition to determine if the loop should execute again, repeating until the condition is false.

for (i = 1; i <= 4; i++) { console.log(i); } console.log("Done!");

1. for loop's initial expression assigns i with 1. 2. for loop's condition is then evaluated. 1 <= 4 is true, so the loop's statements are executed. 3. Output i to the console. 4. After the loop executes, the final expression is evaluated, which increments i. 5.Loop repeatedly executes until i <= 4 is false

What are the first and last numbers output by the code segment? let c = 100; while (c > 0) { console.log(c); c -= 10; }

100 and 10 The loop terminates when c is 0. At the start of the last iteration c is 10. c is then decremented to 0. The loop condition is checked: 0 > 0 is false, and the loop terminates.

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 The inner for loop outputs 2, 3, 4 three times.

What is the last number output to the console? let c = 10; do { console.log(c); c--; } while (c >= 5);

5 The numbers 10, 9, 8, 7, 6, 5 are output. After 5 is output, c becomes 4, and the loop terminates.

What is the last number output to the console? let x = 1; do { let y = 0; do { console.log(x + y); y++; } while (y < 3); x++; } while (x < 4);

5 The outer loop executes when x is 1, 2, and 3. The inner loop executes when y is 0, 1, and 2. So x is 3 and y is 2 the last time both loops execute, producing 3 + 2 = 5.

What numbers are output by the code segment? for (c = 5; c < 10; c += 2) { console.log(c); }

5, 7, 9 c is incremented by 2 each time, and the loop terminates when c is 11.

What is c when the loop terminates? let c = 10; while (c <= 20) console.log(c); c += 5;

The loop never terminates Even though c += 5 is indented, only console.log(c) is in the loop body because the while loop does not have { } around both statements. Therefore, c never increments, and 10 is output repeatedly in an infinite loop.

Developers must be careful to avoid writing infinite loops. An infinite loop is

a loop that never stops executing. Ex: while (true); is an infinite loop because the loop's condition is never false.

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. while (condition) { body }

Two jump statements alter the normal execution of a loop. The break statement _______

breaks out of a loop prematurely. for (c = 1; c <= 5; c++) { if (c == 4) { break; // Leave the loop } console.log(c); // 1 2 3 (missing 4 and 5) }

What condition makes the loop output the even numbers 2 through 20? let c = 2; while (_____) { console.log(c); c += 2; }

c <= 20 The last time the loop outputs a value is when c is 20.

What condition causes the for loop to output the numbers 100 down to 50, inclusively? for (c = 100; ______; c--) { console.log(c); }

c >= 50 The loop terminates when c is 49.

Two jump statements alter the normal execution of a loop. The continue statement _______

causes a loop to iterate again without executing the remaining statements in the loop. for (c = 1; c <= 5; c++) { if (c == 4) { continue; // Iterate again immediately } console.log(c); // 1 2 3 (missing 4) 5 }

Brooke is trying to save enough money in the next ten months to purchase a plane ticket to Australia. Every month Brooke saves $200. Write a for loop that displays how much money Brooke will have saved at the end of each month for the next 10 months.

let monthlyDeposit = 200; let accountBalance = 0; for (monthlyDeposit; monthlyDeposit <= 2250 ; monthlyDeposit **); { console.log(monthlyDeposit) }

Write a while loop that multiplies userNum by 2 while userNum is less than 50, displaying the result after each multiplication. Ex: For userNum = 4, output is: 8 16 32 64 (with each value output on a separate line)

let userNum = 4; // Code will be tested with values: 4, 10 and 60 while (userNum < 50) { userNum *= 2 console.log(userNum) }

Write a condition that executes the do-while loop as long as the user enters a negative number. let num; do { num = prompt("Enter a negative number:"); } while (______);

num < 0 The loop terminates when num is greater than or equal to 0.

What is the value of c when the loop terminates? let c = 10; while (c <= 20); { console.log(c); c += 5; }

the loop never terminates The semicolon after the while condition creates an infinite loop. While loops should never have a semicolon after the condition. No numbers are displayed because a semicolon exists after the while condition. If the semicolon was not present, the loop would execute one more time when c is 20, so the value of c would be 25 when the loop terminates.

Loop body

the set of statements that a loop repeatedly executes.


Related study sets

Geology 101 Chapter 5 and HW 5: Volcanoes

View Set

Intimate Relationships Chapter 7

View Set

Micro Chapter 15 hw and quiz answers

View Set

quiz 1, quiz 2, quiz 3, quiz 4, quiz 5, Quiz 6, quiz 7

View Set