When we create a loop for
in any language, sometimes we don't understand the lines and variables that we develop and why we create them.
Example:
When we create an initial variable (example i
) we understand that it is the hint (better known as index
) of an array or object.
for (let i = 0; i < length; i++) //here your code loop
When we make this sentence (most of the programmers and I include myself) we do not understand.
Why the sign in
<
our loop? What logic represents that the variableíndice
is less than the number of times to repeat?
The cycle
for
is basically an iterative structure to execute the same code segment a desired number of times, this desired number of times is controlled by a condition that will evaluate whether or not it continues executing the internal lines of code. The basic parts areThe initialization serves as the base or beginning of the cycle, that is, for example, if you want to execute some lines of code 20 times in a
for
, you could have many options, I would say infinite variations.Both ways will work. but mostly the initial variable of the cycle is used
for
at 0, this is so that apart from serving as a variableiteradora
it also works as aindice
reference to array positions and since these start from 0 . It is customary to start at0
. Note that more than one variable can be initialized in this part.if
if the result is true then it continues executing and if it is false it exits the loop. In this part you can have different types of comparison and more than one condition always closely following the logical truth tables.Mostly like in initialization, you put the
length
, ie the number of times you want the code to execute or the length of the array so you don't get a type exceptionIndexOfRange
when used as index . But it could just be a variablebooleana
and it will act the same way. (ejm)This second part must always return a value
booleano
, true or false .The increment is mostly used to change the state of the iterator variable, there are also different variations and as in the initialization, it can contain more than
1
incremented variable, In addition to incrementing it can also be decremented. these two can be 1 in 1 (++) or (--) or whatever you wanti+=2
(2 in 2)i-=2
(2 in 2) descending. You have to have some things clear in this part, for this maybe this question can be used (ejm)Normally the expression you see in that loop
for
is used to iterate through arrays, which doesn't mean it's only used for that.This is because you have to remember that arrays start at position 0 .
So, if we have an index
i=0
and we have three elements in our array, if we did the following: iti <= length
would give us aArrayIndexOutOfBoundsException
in Java for example, since it would try to access the elementarray[3]
which doesn't exist, in case we wanted to get its value.In any case, the condition used in the loop
for
must always conform to the conditions of your program and does not necessarily have to be with the symbol<
. Everything will depend on the logic of your program.The type of cycle
for
you present is calledfor tradicional
. According to Wikipedia it was popularized by C programmers.It consists of three parts:
initialization : _
let i = 0;
The initialization declares (and perhaps assigns) any required variables. The type of a variable must be the same if you are using multiple variables in the initialization part.
In your case, what is done is to initialize
i
a0
.the condition :
It is checked and exits the loop if the condition is
false
.In your case the condition would be
i < length;
Here the loop will be executed as long asi
it is less than the value oflenght
... the latter can be any value, not necessarily the size of an array.the increment :
This is done every time the loop ends and repeats itself.
In your case,
i++
indicate in the code that each time the loop repeats, the value ofi
must increase by1
.The diagram of this cycle
for
can be represented as follows:Your for Loop or Loop is made up of 3 Parts and/or Elements
1)
let i = 0; //Indica el Inicio de Tu Ciclo
, It can be a Local Variable declared within the same For as in this case.2) which indicates at what point the For
i < length; // Una condicional
Loop should stop (you can call it if ) while the Condition is met or not, it will continue to Iterate. In your example you are telling it to repeat the Loop while your variable i is less than length3)
i++)
The way you will increment your Variable i in this case from 1 to 1 then your variable will start at 0 and if it meets the condition it is met or not depending on your If logic it will continue to increment to 1 it will validate your if and so on ...