generalbots/botbook/src/04-basic-scripting/keyword-for-each.md
Rodrigo Rodriguez (Pragmatismo) 037db5c381 feat: Major workspace reorganization and documentation update
- Add comprehensive documentation in botbook/ with 12 chapters
- Add botapp/ Tauri desktop application
- Add botdevice/ IoT device support
- Add botlib/ shared library crate
- Add botmodels/ Python ML models service
- Add botplugin/ browser extension
- Add botserver/ reorganized server code
- Add bottemplates/ bot templates
- Add bottest/ integration tests
- Add botui/ web UI server
- Add CI/CD workflows in .forgejo/workflows/
- Add AGENTS.md and PROD.md documentation
- Add dependency management scripts (DEPENDENCIES.sh/ps1)
- Remove legacy src/ structure and migrations
- Clean up temporary and backup files
2026-04-19 08:14:25 -03:00

42 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# FOR EACH Keyword
**Syntax**
```
FOR EACH $var IN $collection
// block of statements
NEXT $var
```
**Parameters**
- `$var` Identifier that will hold each element of the collection during iteration.
- `$collection` An array or iterable expression whose items will be traversed.
**Description**
`FOR EACH` iterates over every element of the supplied collection, assigning the current element to the loop variable `$var` for the duration of the block. The block is executed once per element. After the loop finishes, execution continues after the matching `NEXT $var` statement.
If the collection is not an array, the keyword raises a runtime error indicating the expected type.
**Example**
```basic
SET numbers = [1, 2, 3, 4, 5]
FOR EACH n IN numbers
TALK "Number: " + n
NEXT n
TALK "All numbers processed."
```
The script outputs each number in the list sequentially and then prints a final message.
**Control Flow**
- `EXIT FOR` can be used inside the block to break out of the loop early.
- Nested `FOR EACH` loops are supported; each must have a distinct loop variable.
**Implementation Notes**
- The keyword evaluates the collection expression once before entering the loop.
- The loop variable is scoped to the block; it does not affect variables outside the loop.