Skip to content
On this page

Statements: For

The for statement is used to execute a block of code a specified number of times.

syntax
ForStatement :
  for (Iterator | ( ( Iterator ) ) ) BlockStatement
 
Iterator :
  Initializer? ; Condition? ; Increment?
 
Initializer :
  Expression | (Expression , Initializer)
 
Condition :
  Expression | (Expression , Condition)
 
Increment :
  Expression | (Expression , Increment)


for $i = 0; $i < 10; $i = $i + 1 {
    // ...
}

A for statement can have multiple expressions in the initializer, condition, and increment sections.

for $i = 0, $j = 0; $i < 10 && $j < 10; $i++, $j++ {
    // ...
}

A for statement can have an empty initializer, condition, or increment section.

// Iterate until $i is greater than 10, incrementing $i with each iteration
for ; $i < 10; $i++ {
    // ...
}

// Infinite loop, incrementing $i with each iteration
for ;; $i++ {
    // ...
}

// Infinite loop
for ;; {
    // ...
}

Licensed under either of the MIT License or the Apache License (Version 2.0), at your option.