STEP: Your FOR Loop's Pace Setter

STEP: Your FOR Loop's Pace Setter

Want more control over how your FOR loops stride through their iterations? Meet STEP, the keyword that lets you fine-tune the pace of your loops! While the default step is 1, STEP lets you customize the increment (or decrement) for your loop counter, opening up a world of possibilities for more flexible and expressive loops.

Syntax

FOR <counter> = <start> TO <end> STEP <increment>
    ' Code to be repeated
NEXT <counter>

Where: - <counter>: The numeric variable that keeps track of the loop's progress. - <start>: The initial value of the counter. - <end>: The final value of the counter. - <increment>: The amount by which the counter is increased (or decreased if negative) after each iteration.

Applications

The STEP keyword is your key to:

  • Counting by different values: Easily count by 2s, 5s, 10s, or any other increment you desire.
  • Counting backwards: Use a negative increment to decrement your counter and loop in reverse.
  • Creating fractional steps: Work with decimal increments for finer control over loop iterations.
  • Generating sequences: Build custom number sequences for calculations, simulations, or even musical patterns.

Code Examples

1. Counting by Twos:

10 FOR I = 0 TO 10 STEP 2
20    PRINT I;     :rem Output: 0 2 4 6 8 10
30 NEXT I

This loop counts from 0 to 10 in increments of 2.

2. Counting Backwards:

10 FOR J = 10 TO 1 STEP -1
20    PRINT J;     :rem Output: 10 9 8 7 6 5 4 3 2 1
30 NEXT J

This loop counts down from 10 to 1.

3. Fractional Increments:

10 FOR K = 0 TO 1 STEP 0.25
20    PRINT K;     :rem Output: 0 0.25 0.5 0.75 1
30 NEXT K

This example demonstrates how to use decimal increments.

STEP in the Wild: The Pixelated Animator

Imagine you're creating an animation where a sprite moves across the screen. You could use a FOR loop with a fractional STEP value to control the speed and smoothness of the sprite's movement.

Don't be limited by the default step! With STEP, you can customize the pace of your FOR loops, opening up new possibilities for creative coding and precise control over your programs. It's like having a gear shifter for your loops, allowing you to adjust their speed and direction as needed. So embrace the versatility of STEP and let your loops dance to your own rhythm!


Privacy Policy

Let's be honest, we collect some data. But we promise not to sell it, we'll mostly use it to improve our service. You can read the nitty-gritty details below, but be warned, it uses big words that lawyers like (we don't judge).

By using our service, you agree to this privacy policy. This disclaimer strikes a balance between lightheartedness and informing users that they should still read the full privacy policy if they choose.