For/Next/Step
Posted: Sat Apr 18, 2020 7:21 am
Can some nice programmer enlighten me on the usage of STEP in a FOR - NEXT loop? I've been trying the following code running on the MS-DOS 6.22 distribution of QBASIC (whatever version that is????).
For A = 1 TO 128 STEP A^2
PRINT A
NEXT
To try to achieve the screen output of:
1
2
4
8
16
~~~
128
Instead I get this:
1
1
1
1
1
~~~~ etc,
I ended up doing this:
For A = 1 TO 128
PRINT A
A = A * 2 -1 ' Definitely need the -1. NEXT instruction increments +1
NEXT
Ditching the use of STEP altogether. This works, but I just wonder why the other way doesn't? I am reasonably sure that I made the former way work years ago, but I was probably using QBX 7 then. Maybe I am forgetting something about the way it should be formatted. I've tried round brackets (A^2) and nothing changed. Anyway, it's working with method 2.
Thanks for the love <3
Jj
For A = 1 TO 128 STEP A^2
PRINT A
NEXT
To try to achieve the screen output of:
1
2
4
8
16
~~~
128
Instead I get this:
1
1
1
1
1
~~~~ etc,
I ended up doing this:
For A = 1 TO 128
PRINT A
A = A * 2 -1 ' Definitely need the -1. NEXT instruction increments +1
NEXT
Ditching the use of STEP altogether. This works, but I just wonder why the other way doesn't? I am reasonably sure that I made the former way work years ago, but I was probably using QBX 7 then. Maybe I am forgetting something about the way it should be formatted. I've tried round brackets (A^2) and nothing changed. Anyway, it's working with method 2.
Thanks for the love <3
Jj