FOR

For is one the most versatile and most widely used looping mechanism across most programming languages.

FOR (initialize counter; evaluate counter; increase counter) {
    // code to execute for each iteration
}

Example;

$result =  ''
FOR ($x = 0; $x < 10; $x++) {
    $result .= html:p('$x is ' . $x)
}
$result
play_arrow RUN   open_in_new OPEN IN TAB

Here is how the above code works:

  1. Initialize counter statement of $x = 0; initializes the $x and sets the starts the loop with value of $x set to 0.
  2. Evaluate counter statement of $x < 10; will check the validation of the expression and as long as the $x value is less than 10 will continue the loop.
  3. Increase counter statement of $x++; will increase $x by 1 after each iteration.

Copyright ©2013-2022 SunSed®. All rights reserved.