Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

How a for Loop Takes Its Steps

In this video, you packed a while loop into a for loop and followed it one pass at a time. The key points:

  • A for loop holds the same three parts as a while loop, the start, the check and the update, in one header line, separated by semicolons.
  • The start runs once, before anything else. Then the loop repeats check, body, update, until the check is false.
  • The update runs after the body and before the next check.
  • The update decides which way the counter moves and how far: round++ counts up by one (short for round = round + 1), count-- counts down by one (short for count = count - 1), and score += 10 takes steps of 10 (short for score = score + 10).
  • The check comes before every pass, the first one included, so a for loop can run zero times.

The rounds loop from the video:

Language: JavaScript
for (let round = 1; round <= 3; round++) {
  console.log("Round", round);
}
console.log("Game over");
Language: Program Output
Round 1
Round 2
Round 3
Game over

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your guessing game will run in rounds, so it’s time to count them. Here’s a while loop that counts three, the kind you wrote in the last two lessons. Run the file, and the terminal shows Round 1, Round 2 and Round 3, followed by Game over.

00:16 It works, but look at where its three parts live. The start sits above the loop. The check sits in the while line. And the update hides at the bottom of the body. round++ is short for round = round + 1.

00:34 Forget that last line, and round stays at 1 until you press Ctrl+C. Wouldn’t it be nicer to see all three parts in one place?

00:44 That’s what a for loop gives you. It starts with the keyword for and a pair of parentheses. The start moves up first, into the parentheses, and a semicolon follows it.

00:57 The check moves in next, with another semicolon after it. Last, the update leaves the body and slides into the end of the header, with no semicolon after it.

01:09 Now the body only holds the real work. Run it, and you get the same four lines as before. A for loop is a while loop with its three parts packed into one line.

01:23 So in which order do those parts run? Follow round one pass at a time. The start runs once, before anything else. Now round is 1. The check asks whether round is at most 3.

01:39 It is, so the body runs and logs Round 1. The body is done. Which part of the header runs next? Your turn: pause the video and predict the next step.

01:56 It’s the update. Round becomes 2, and only then does the check run again. Two is at most 3, so you get Round 2. The update makes round 3, and you get Round 3.

02:12 Then round becomes 4. This time the check says false, the loop ends, and the line after it logs Game over.

02:23 The update decides which way the counter moves, and how far. Maybe your game counts down before the first round. This loop starts count at 3, keeps going while count is at least 1, and counts down with count--, short for count = count - 1.

02:41 The terminal shows 3, 2 and 1, and then Go. Say each round is worth 10 points. Here, score starts at 0, keeps going while score is at most 30, and steps up with score += 10, short for score = score + 10.

03:02 That gives you Score 0, 10, 20 and 30. Four passes, because 0 gets a pass, too.

03:11 Back to your rounds. What if a player picks zero rounds? Now the check asks whether round is at most 0.

03:20 How many rounds will this loop log? Your turn: pause the video and predict the number.

03:30 None at all. The start sets round to 1, and the check fails on its very first try. So the body never runs, and neither does the update. The terminal shows only Game over.

03:46 A for loop checks before every pass, the first one included, so it can run zero times.

03:54 Here’s the order in one line: start once, then check, body and update, again and again, until the check says false.

04:03 Nice work. You’ve packed a while loop into a for header and followed it counting up, counting down and in steps. Next, in Count Totals and Characters With for, you’ll put this loop to work adding up totals and walking through a word’s letters.

Become a Member to join the conversation.