Chapter 4 suggestion - improving the break keyword example

I suggest a more complete example below:

var sum = 1;
while (true) {
  print(sum);
  sum += 4;
  if (sum > 10) {
    break;
  }
}
// 1, 5, 9 will be printed

In addition, many examples start with say, sum = 2, I suggest change to var sum = 2

For each key concept explained in the book, the example code blocks follow on from each other.

The sum variable is declared in the first example of the while loop.

var sum = 1;
while (sum < 10) {
  sum += 4;
  print(sum);
}
3 Likes

Hi Laura, thank you for the explanation. I see the reason behind now.

1 Like