After the proper explanations and operation of the FOR loop, both in javascript and PHP (to emphasize the similarity of the two), see at the end of this reply, your code commented and running passo a passo
in the ideone.
The for statement creates a loop consisting of three optional expressions, within parentheses and separated by semicolons, followed by a statement or a sequence of statements executed in sequence.
for ([inicialização]; [condição]; [expressão final])
initialization
An expression (including assignment expressions) or variable statements. Usually used to start the variable counter. This expression can optionally declare new variables with the var keyword. These variables are not local in the loop, i.e., they are in the same scope as the for loop is. The result of this expression is discarded.
condition
An expression to be evaluated before each loop iteration. If this expression is evaluated to true, statement will be executed. This condition test is optional. If omitted, the condition will always be evaluated as true. If the expression is evaluated as false, the execution will go to the first expression after the loop construction is.
final expression
An expression that will be evaluated at the end of each loop iteration. This occurs before the next condition assessment. Usually used to update or increment the counter variable.
statement
A statement that is executed as long as the condition is true. To perform multiple conditions within the loop, use a block instruction ({...}) to group these conditions. To not execute statements within the loop, use an empty statement (;).
Examples of use
The declaration starts by declaring the variable i and initializing it as 0. It checks if i is less than nine, executes the two subsequent instructions and increments 1 variable i after each passage through the loop.
for (var i = 0; i < 9; i++) {
console.log(i);
}
All three expressions in the for loop condition are optional.
For example, in the boot block, it is not necessary to initialize variables:
var i = 0;
for (; i < 9; i++) {
console.log(i);
}
As with the boot block, the condition is also optional. If you are omitting this expression, you should make sure to break the loop in the body so as not to create an infinite loop.
for (var i = 0;; i++) {
console.log(i);
if (i > 3) break;
}
You can also omit all three blocks. Again, make sure to use a break instruction at the end of the loop and also modify (increment) a variable, so the break condition is true at some point.
var i = 0;
for (;;) {
if (i > 3) break;
console.log(i);
i++;
}
SOURCE
With PHP It’s very similar, see
Examples of use
The declaration starts by declaring the variable i and initializing it as 0. It checks if i is less than nine, executes the two subsequent instructions and increments 1 variable i after each passage through the loop.
In this first example we have our common FOR, as we all learn:
<?php
for($i = 0; $i < 5; $i++)
{
echo $i;
echo '<br />';
}
In this second example, we take the first parameter from FOR, and leave the $i variable defined outside the loop:
<?php
$i = 0;
for(; $i < 5; $i++)
{
echo $i;
echo '<br />';
}
Now, let’s take the third parameter, and increment the variable within the loop.
<?php
$i = 0;
for(; $i < 5;)
{
echo $i;
echo '<br />';
$i++;
}
Finally, we will remove the second parameter.
<?php
$i = 0;
for(; ; )
{
echo $i;
echo '<br />';
if($i == 5)
{
break;
}
$i++;
}
Want to see to believe? São Tomé test on ideone
Note that in all of these examples above, we escape the Infinite Loop with some condition or mechanism that we put inside the loop. Be careful. This last example if nothing is done inside the loop, easily generates an infinite loop.
Your commented code
$a = true;
$b = -2;
$c = 7;
$condicao="";
//realiza uma iteração FOR toda vez que $b for menor que $c ou $a for true
//retiramos o primeiro parâmetro do FOR, a variável $b já foi definida fora do laço
for ( ; $b < $c || $a; $b++){
if ($c + $b * 2 > 20)
// quando a condição acima for verdadeira, $a se torna false
$a = false;
//e a condição FOR já não será mais verdadeira, isto é, zefini, ou seja, "c'est fini"
echo $b." ";
}
Follow the step by step of your code on IDEONE
The variable
$b
is already stated, so it would be the same asfor($b = -2; $b < $c || $a; $b++)
– NoobSaibot
Do you have other parts of this code or is this all? Change everything if you have other parts.
– Maniero
is just that. But because the dot is comma at the beginning of the is ? if the delete returns an error
– 1001Songs Clips
But the problem wasn’t the first statement of
for
? You switched acceptance to an answer that doesn’t even touch the subject?– Maniero