Abod
5 min readMay 1, 2020

Understanding Loop In jS and any language(Everything you need to know about loop)

I got few requests about understanding loop , I decided to write about it , firstly let’s look at what loop is

might be while , do or for loop. We are using the bellow format to describe the types of loops generally (while loop , for loop and do while loop) , When using loop to increment and decrement you might be wondering why i = 0 ; i < 10; i++ works and why i = 0; i > 10 ; i++ doesn’t .

People believe having very deep understanding and knowledge on a topic like how it works , why it works the way it does helps in mastering the topic , I also tried understanding why i=10 ; i < 10; i — doesn’t work , my brain counted to -1million before it did shut down and I woke with an headache , let’s go back to , i = 0 ; i < 10 ; i++ ; let me break it down i=0 , we are assigning 0 to i, i < 10 is the condition which tells the loop to continue until when the equation evaluates to false then it breaks out of the loop and stop . i++ is the statement which tells the loop we want to increment i ,that is we are adding 1 each time until the condition is met or until it becomes untrue ,without the statement the loop would continue to loop until it becomes less than 10 which is impossible, it would become an endless loop .

i = 0 ; i < 10 ; i++ this says we want to increment i until be becomes less than 10 ,it’s adding 1 until the statement becomes untrue , when it gets to 9 it’s less than 10 , the statement becomes untrue so it’s stops .

i = 0 ; i > 10 ; i++ if youre still wondering why this doesn’t work , let me break it down , it says i is 0 and we want the condition i > 10 to continue looping until the condition becomes false , there is no end to the number being greater than 10 even a million is greater than 10 , people keep on asking why it doesn’t stop at 11 and i < 10 stops at 9 , basically it’s because 10 is like a basic stop , although all numbers below are also less than 10 but the statement becomes untrue wants it gets to 10 (basic stop) when we say i > 10 this statement wouldn’t become untrue because there is no basic stop and therefore it can’t stop at 11 , I’ll give an example , you’re driving a car and on the road you see a stop sign or a block the next thing you would do is stop your car before the stop sign or the block (that’s why I call the elements after the < a basic block) , but if there is no stop sign or block in front of you you’re going to continue driving and accelerating , and the condition would not become untrue.

that’s why i = 0 ; i < 10 ; i++ works and i = 0; i > 10 ; i++ doesn’t.

Decrementing your number

i = 10 ; i < 0 ; i —

this won’t work because it doesn’t have a basic block so it’s going to become an [] (empty array) but

i = 10 ; i > 0 ; i —

this statement decreases 1 from 10 until it becomes less than 10 (In which the statement would become false), that is , it should stop before hitting the basic block which is 0 . This would help you understand loop a bit and also teach you how to increment and Decrement numbers using loop .

let’s also check the difference between logging inside the kali braces and outside the kali braces

i = 0 ; i < 10 ; i++ {

conlose.log(i);

}

and

i = 0 ; i < 10 ; i++ {

}

console.log(i);

we are using the above format to describe the types of loops generally (while loop , for loop and do while loop).

For loop should be like this

for ( i = 0 ; i < 10 ; i++) {

}

and while loop

var i = 0 ;

while (i < 10); {

i++;

}

we are using the above format to describe the types of loops generally (while loop , for loop and do while loop).
For loop should be like this
for ( i = 0 ; i < 10 ; i++) {
}
and while loop
var i = 0 ;
while (i < 10); {
i++;
}
if I do this
i = 0 ; i < 10 ; i++ {
conlose.log(i);
}it would print the number
1
2
3
4
.
. continuously till it becomes false because we have the console.log inside of the loop so it’s prints the number continuously till it becomes false
but If i do this
i = 0 ; i < 10 ; i++ {
}
conlose.log(i);
it would only print out a number , in this case it’s, 10 that’s the final number , that’s why we use this method to push numbers inside of an array , it would give us [1,2,3,4,5,6,7,8,9]
but if we place our console.log inside of the loop and we want to push numbers inside of an array using push() , it would give
[1]
[1,2]
[1,2,3]
.
.
would run till the equation becomes false .
The two does different things depending on what u want to archive.