DIM A AS _UNSIGNED _BYTE
A = 255
DIM B AS INTEGER
FOR B = 0 TO A
PRINT "hI"
NEXT B
PRINT B
'A better FOR NEXT work around
DO
IF A = 0 THEN EXIT DO
'***** code *****
IF B = A THEN EXIT DO
B = B + 1
LOOP
PRINT B ' = 255 exactly as you'd expect.
FOR X = 0 to 1No, it executes twice.
'CODE
should execute code only once - it does
from Start (included) to End (included) Step (default 1)while it seems that your vision is
do this
next (again)
from Start (excluded) to End (included) step (default 1)
do this
next (again)
from Start to End Step (default 1)
it increases counter here and then it tests if is in the range Start End
do this
next (again)
DIM A AS _UNSIGNED _BYTE
A = 1
DIM B AS INTEGER
FOR B = 0 TO A
PRINT "hI"
NEXT B
PRINT B
B = 0
DO
PRINT "hi2"
IF B = A THEN EXIT DO
B = B + 1
LOOP
PRINT B ' = 1 exactly as you'd expect.
introduction of spaghetti code is never an improvement or better.
DIM A AS _UNSIGNED _BYTE
A = 255
'DIM B AS INTEGER
DIM B AS _UNSIGNED _BYTE
B = 0
DO
PRINT "hi"
B = B + 1
IF B > A THEN EXIT DO
IF INKEY$ = CHR$(27) THEN EXIT DO
LOOP
PRINT B
END
DIM A AS _UNSIGNED _BYTE
A = 255
'DIM B AS INTEGER
DIM B AS _UNSIGNED _BYTE
B = 0
DO
PRINT "hi"
B = B + 1
if B = 0 then EXIT DO ' or maybe assembler instruction LOOPNZ etc
IF B > A THEN EXIT DO
LOOP
PRINT B
END
DIM A AS _UNSIGNED _BYTE
A = 255
'DIM B AS INTEGER
DIM B AS _UNSIGNED _BYTE
B = 0
DO
PRINT "hi2"
IF B = A THEN EXIT DO
B = B + 1
LOOP
PRINT B ' = 255 exactly as you'd expect.
Not see'n where that's ("spaghetti code") coming from???
Code: QB64: [Select]
spaghetti code, kind of hard to miss man.
It's so basic....
'Dont forget that the for/next command stops when x becomes > ITERATION
START = 0
ITERATION = 10
County = 0
For x = START To ITERATION
County = County + 1
Next x
Print "County="; County; " and X="; x; " Calculated steps for next here is :"; (ITERATION - START + 1); ""
Cobalt is a purist.
Cobalt is a purist.
Not a single GOSUB let alone a GOTO must be talk'n from Cobalt Code World again.
If you are dissing EXIT DO again that saves us from spaghetti code, at least in bplus Code World ;-))
EXIT loop is an elegant way out of a loop. I don't think that's just my opinion, I think the majority of people who complained of spaghetti code would agree that it sure beats a GOTO out of loop.
@Cobalt
I made some mistakes in the code but corrected them. FOR NEXT loops work well enough. FOR NEXT loops must have been coded 50 years ago and they work fine but the counter variable, after exit, is always one more than the limit (or 0 in the case of a wrap around) and it does not have to be that way.
I can't think of any good reason for it.
@bplus : AS a Purist, any command that causes the the code to jump out of line is spaghetti code. And there is never a need for it.
Unless we were programing in assembler which would leave no choice. But BASIC is a high enough programing language we can completely avoid it in our code.
OK so this is the action of the usual FOR NEXT loop
notice that things go wrong when B is a _UNSIGNED _BYTE
B has a value of 255 and then is incremented and so now equals zero. The FOR NEXT goes into an endless loop but as we know that does not happen - confused?
Hit ESCAPE key if you run the code
I suppose whoever coded the original FOR NEXT loop noticed that wrap around, so had to add extra code to prevent the endless loop.
However, the following code has only one IF condition and after exit the B counter has a value that matches A
Here follows what would be considered a trivial example of spaghetti code in BASIC. The program prints each of the numbers 1 to 100 to the screen along with its square. Indentation is not used to differentiate the various actions performed by the code, and the program's GOTO statements create a reliance on line numbers. The flow of execution from one area to another is harder to predict. Real-world occurrences of spaghetti code are more complex and can add greatly to a program's maintenance costs.Code: QB64: [Select]
1 i=0 2 i=i+1 7 END
Here is the same code written in a structured programming style:Code: QB64: [Select]
The program jumps from one area to another, but this jumping is formal and more easily predictable, because for loops and functions provide flow control whereas the goto statement encourages arbitrary flow control. Though this example is small, real world programs are composed of many lines of code and are difficult to maintain when written in a spaghetti code fashion.
This program from the 1970's is TRUE spaghetti code:
Also the counter variable stays incremented after the FOR NEXT exits. So If A = 255, B = 256 after the exit. There are many times when this has caused hard to trace bugs.
I generally find DO...LOOP and FOR...NEXT to be nearly interchangeable, and I tend to go for DOs more often, as I've heard it said that they are somewhat faster. I try to see what gets the job done in the least amount of code, using the philosophy that the best and most reliable part is no part.
the DO-LOOP cycle, not only often exites with EXIT DO without reaching the condition of LOOP UNTIL, LOOP WHILE, DO UNTIL, DO WHILE, but it can also loops only a part of itself. FOR-NEXT, it is very powerfull to make arrays for math purposes.you can evaluate this new opportunity that is of QB64 and not of Qbasic
@bartok
about you can evaluate this new opportunity that is of QB64 and not of Qbasic
http://qb64.org/wiki/CONTINUE (http://qb64.org/wiki/CONTINUE) together this one http://qb64.org/wiki/EXIT (http://qb64.org/wiki/EXIT)