CodingNic

Control Flow

Exercises

Control Flow 30 min read

Exercises

This chapter introduces no new concepts. It’s a set of exercises that pull together everything from this module: if/else if/else, switch, for, while, do...while, break, continue, and nested loops. Write and run each one in a file with node, and check your output against what’s shown.

  1. Declare const temperature = 28;. Write an if/else that logs "Warm" if temperature >= 25, otherwise "Cool". Expected output: Warm

  2. Declare const score = 55;. Write an if/else if/else chain that logs "Pass" if score >= 60, and "Fail" otherwise. Expected output: Fail

  3. Declare const hour = 9;. Write an if/else if/else chain that logs "Morning" if hour < 12, "Afternoon" if hour < 18, and "Evening" otherwise. Expected output: Morning

  4. Declare const day = 3;. Write a switch on day with cases 1 through 5 logging "Weekday", and a default logging "Weekend". Remember break in every case. Expected output: Weekday

  5. Declare const day = 6;. Reuse the same switch structure from exercise 4. Expected output: Weekend

  6. Write a for loop that logs the numbers 1 through 10, each on its own line. Expected output: 1 through 10, one number per line.

  7. Write a for loop that logs only the even numbers from 2 to 20 (use i % 2 === 0 to check). Expected output: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, one per line.

  8. Declare let total = 0; and let n = 1;. Write a while loop that adds n to total and increments n by 1 each time, stopping once n > 5. Log total after the loop. Expected output: 15 (this is 1 + 2 + 3 + 4 + 5).

  9. Declare let balance = 500;. Write a do...while loop that logs "Withdrawal" and subtracts 100 from balance, continuing while (balance > 0). Expected output: Withdrawal printed 5 times.

  10. Declare let count = 0;. Write a do...while loop with the condition while (count > 0) that logs "Running" and decrements count. Expected output: Running printed exactly once, proving the body runs at least once even though the condition starts out false.

  11. Write a for loop from 1 to 30 that uses break to stop as soon as it reaches a number greater than 7. Expected output: 1, 2, 3, 4, 5, 6, 7, one per line, then nothing more.

  12. Write a for loop from 1 to 10 that uses continue to skip the number 5 only, logging every other number. Expected output: 1, 2, 3, 4, 6, 7, 8, 9, 10, one per line.

  13. Write a nested loop that prints a 5 by 5 multiplication table. The outer loop should run row from 1 to 5, and for each row, the inner loop should build one line of text by multiplying row * col for col from 1 to 5 (append each result plus a space onto a string with +, the same pattern used in this module’s loops lesson), then log that line. Expected output: 5 lines, where the first is 1 2 3 4 5 and the last is 5 10 15 20 25 .

  14. Declare const grade = 85;. Write an if/else if/else chain assigning a letter grade: "A" for 90 and above, "B" for 80 to 89, "C" for 70 to 79, and "F" below 70. Store the result in a variable and log it. Expected output: B

If any of your answers don’t match the expected output shown, re-read the relevant section of this module before moving on. Every exercise here is meant to be checkable just by running it.

Next module: Functions, where you’ll learn to package code like this into reusable, named blocks instead of writing it out every time.