for
Thefor
loop is a control flow statement that allows code to be executed repeatedly based on a condition. Soul supports the classic C-style for loop with initialization, condition, and increment components.
Basic For Loop
The standard for loop syntax with initialization, condition, and increment:For Loop Components
A for loop consists of three parts:Different Initialization Patterns
Various ways to initialize the loop variable:Different Increment Patterns
Various ways to modify the loop variable:For Loops with Arrays
Iterating through arrays using index:For Loops with Strings
Iterating through string characters:Nested For Loops
For loops can be nested for multi-dimensional processing:For Loops with Complex Conditions
Using complex conditions and multiple variables:For Loops with Break and Continue
Controlling loop execution:For Loops in Data Processing
Common patterns for processing data:For Loops with Functions
Calling functions within for loops:For Loops with Objects
Iterating through object properties using indices:Infinite For Loops
Creating infinite loops (use with caution):For Loops with Error Handling
Handling errors within for loops:Performance Considerations
Optimizing for loop performance:Best Practices
- Use meaningful variable names:
index
instead ofi
when clarity is important - Cache array lengths: Store length in a variable for better performance
- Use appropriate increment: Choose the right increment pattern for your needs
- Handle edge cases: Check for empty arrays or invalid indices
for
loop is essential for controlled iteration and is particularly useful when you need access to the current index or want precise control over the iteration process.