Just use i+= 0.5
in the last part of loop.
for(var i = 1; i <= 10; i+= 0.5) {
console.log(i)
}
A for
loop has 3 parts:
for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement
When a for loop executes, the following occurs:
- The initializing expression initialExpression, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression of any degree of complexity. This expression can also declare variables.
- The conditionExpression expression is evaluated. If the value of conditionExpression is true, the loop statements execute. If the value of condition is false, the for loop terminates. (If the condition expression is omitted entirely, the condition is assumed to be true.)
The statement executes. To execute multiple statements, use a block statement ({ ... }) to group those statements.
- If present, the update expression incrementExpression is executed.
Control returns to Step 2.
So you can set any incrementExpression
you want, like += 0.5
or anything else.
Loops and iteration