Skip to content

For

For loop can traverse each element in the specified container. It will call the iter method of the container to create an iterator.

The following code for loop traverses all elements in the array. After the loop ends, count is equal to 45.

nvs

Range

range type also implements the iter method. You can specify the initial value, end value, step value for it. The step value defaults to 1.

The following code for loop also calculates count equal to 45.

nvs

And when you set the step value to 3, the loop ends and count is equal to 18.

nvs

WARNING

In a for loop, you can't use stateful functions, such as ma, sum.