Skip to content

1.12 Loops -3-

More Practice

Task 1: Display the square of numbers from 0 to 10 in the console.

for(var num=0; num<=10; num++){
  console.log(num*num);
}

The result will be:

0
1
4
9
16
25
36
49
64
81
100

Well done! Note that: I used for loop here because I found it more convenient than while loop as I know the number of iterations.

Task 2: Display the square of numbers from 0 to 10 in the console. If the number is 5, display the double of that number.

for(var num=0; num<=10; num++){
  if(num === 5){
    console.log(5 * 2);
  }else{
    console.log(num*num);
  }
}

The result will be:

0
1
4
9
16
10
36
49
64
81
100

Task 3: Display the square of numbers from 0 to 10 in the console. If the number is 5, do not display it at all!

for(var num=0; num<=10; num++){
  if(num === 5){

  }else{
    console.log(num*num);
  }
}

The result will be:

0
1
4
9
16
36
49
64
81
100

What is going here?

If the number is 5, simply do nothing. So I wrote nothing.

We can write comments instead of nothing, just to help other programmers to understand our code. We can re-write the code like this:

for(var num=0; num<=10; num++){
  if(num === 5){
        //Do nothing
  }else{
    console.log(num*num);
  }
}

// is a sign of JavaScript single line comment. Whatever you write after // will not be executed. Think of comments as notes added to our program. If you want to write multiple line comment, use /* your comment here*/

Comments

Task 4: Add a single line comment.

// This is a single line comment. This line will not be executed. Yaay!

Task 5: Add a multi-line comment.

/*
This is a multi-line comment
This will not be executed.
I can write anything I want
I can add whatever I like here
But I will be moderate in writing comments
I will add them as notes to myself and other programmers
to clarify why I did a certain task in a certain way.
*/

Continue Statement

Task 6: Compare the following code with the code in Task 3.

for(var num=0; num<=10; num++){
  if(num === 5){
      continue;
  }
  console.log(num*num);
}

The result will be:

0
1
4
9
16
36
49
64
81
100

You can see that this code achieves the same result achieved in task 3. However, we used continue here. continue means that do nothing and skip this iteration. Let's practice more:

Task 7: Divide the numbers (-50,-40,-30, -20,-10, 0, 10, 20, 30, 40, 50) over 10 but skip 0.

for(var num=-50; num<=50; num=num+10){
  if(num === 0){
      continue;
  }
  console.log(num / 10);
}

The result will be:

-5
-4
-3
-2
-1
1
2
3
4
5

Task 8: Count from 1 to 5 but skip 3. Hint: use while loop.

var counter = 0;
while (counter < 5) {
   counter++;
   if (counter === 3) {
      continue;
   }
   console.log(counter);
}

The result will be:

1
2
4
5

This example is a bit tricky, let's try to understand it:

  1. The counter is set to 0.
  2. The loop will go on as long as the counter is less than 5.
  3. The counter is increased by 1, so the counter value is 1 now.
  4. Is the counter value equal to 3? No!
  5. display the counter value 1 in the console.
  6. The counter is increased by 1, so the counter value is 2 now.
  7. Is the counter value equal to 3? No!
  8. Display the counter value 2 in the console.
  9. The counter is increased by 1, so the counter value is 3 now.
  10. Is the counter value equal to 3? Yes! Skip this iteration.
  11. The counter is increased by 1, so the counter value is 4 now.
  12. Is the counter value equal to 3? No!
  13. Display the counter value 4 in the console.
  14. The counter is increased by 1, so the counter value is 5 now.
  15. Is the counter value equal to 3? No!
  16. Display the counter value 5 in the console.
  17. 5 < 5 is false, then break out of the loop.

Note(1): Update the while loop counter before using the continue statement. Otherwise, the program will hang.

Task 9: Add 2 to every number from -4 to 4. Skip the number -4 and 4. Do not forget to break out of the loop if the resultant number is greater than or equal to 4.

for(var num=-4;num<=4; num++){
  if(num === -4 || num === 4){
    continue;
  }
  var add2 = num + 2;
  if(add2 >= 4){
    break;
  }
  console.log(add2);
}

The result will be:

-1
0
1
2
3

Do not worry if you do not understand task 8 and task 9 very well. Just read the code again and try to write a similar code. keep on practicing and you will get the most of it.