diff --git a/loops/comparisons.md b/loops/comparisons.md new file mode 100644 index 00000000..13baa331 --- /dev/null +++ b/loops/comparisons.md @@ -0,0 +1,65 @@ +# Comparing the loops + +If you have read about the for and while loops, you will discover that a while loop is much the same as a for loop, with some minor differences. +Let's see a sample case and write the for, while and do/while loops for the same case. + +Let's consider the following array: + +```javascript +var cars = ["BMW", "Volvo", "Saab", "Ford"]; +``` + +- ### For loop +The loop in this example uses a for loop to collect the car `names` from the `cars` array in the `text` string: + +```javascript +text = ""; + +for(var i = 0; i < 4; i = i + 1){ // We use the condition `i<4` as we know `cars` has 4 objects + text = text + cars[i] + " "; // Adds the name in the string and a space " " character after every name +} + +cout<