Step Indicator in For/Next Loop

dlfallen

Active Member
Licensed User
Longtime User
How do you specify the step indicator in a For/Next Loop in B4A?

in B4PPC I used:
For I = DF-2 to 2 step -2

the documentation for B4A says the syntax should be:
For variable = value1 To value2 [Step interval]

So far I have tried (with no success):
For I=DF-2 To 2 step -2
For I=DF-2 To 2 -2
For I=DF-2 To 2, -2
For I=DF-2 To 2 [-2]
For I=DF-2 To 2 (-2)
For I=DF-2 To 2, [-2]
For I=DF-2 To 2, (-2)
For I=DF-2 To 2, step -2
For I=DF-2 To 2 (step -2)
For I=DF-2 To 2, (step -2)
For I=DF-2 To 2, step(-2)
For I=DF-2 To 2 step(-2)

What is the secret?
 

derez

Expert
Licensed User
Longtime User
the first line should work, check for some other reason for not working...
B4X:
For I=DF-2 To 2 step -2
'do something
next
 
Upvote 0

dlfallen

Active Member
Licensed User
Longtime User
Thanks! Knowing the correct syntax made all the difference in the world. Once I could rely on that, I could spend my efforts (as you suggested) on finding the REAL cause of the error messages.

Turns out the sub started with (something like):
Sub demo(DF)

in the calling code, DF was read from an EditText. The Sub was balking because in the code "For i = DF . . ." the compiler was treating DF as a string, not as a number. I solved this with a small change to the Sub:
Sub demo(DF as Int)

In time I would hope that all the core keywords would get more complete examples. It was not at all obvious to me what the correct form of the Step instruction was. In the meantime, it is great to have folks like yourself so willing to point me in the right direction.

THANKS! :)
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
because in the code "For i = DF . . ." the compiler was treating DF as a string, not as a number.
I'd like to see a bit more detail on that problem if you have it because the string should have been automatically coerced to a number as long as the contents of the string were a valid representation of a number in the first place.
 
Upvote 0

dlfallen

Active Member
Licensed User
Longtime User
OK, attached is a simple program that replicates the problem.
Sub Demo(df) produces an error message when compiling.
Sub Demo(df as Int) does not produce the error message.

Running this simplified program, and reading the error message more closely, it appears that indeed the string is converted to a number. But the converted number is double precision and the loop index requires an integer. At least, that's what I get out of the error message.
 

Attachments

  • TestProg.zip
    5.6 KB · Views: 298
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would suggest you to always declare ALL your variables.

In your case df is admiited as a String variable.
A String variable is not accepted in a For/Nect loop.
The loop index can be either Integer or Double.

Attached, your program with Integer, Double and mixed variable types.
It's up to you define what type your variables must be.

Best regards.
 

Attachments

  • TestProg.zip
    5.7 KB · Views: 275
Last edited:
Upvote 0

dlfallen

Active Member
Licensed User
Longtime User
Thank Klaus and Andrew. I faced that problem in several other parts of my program as well. Klaus recommends declaring all variables. That's a nice thought, but my program is math intensive and includes code translated from C, Fortran, BASIC, and psuedo code (if I were a genius, I would have written it from scratch!). To declare all of the variables would require a lot of time cataloging the many variables and tracking down what it is they do. A lot of people curse BASIC because it is to undiciplined, but that is one of it's attractions as well. You don't have to be anal to use it.

A related question:
For i = 1 to 10
Next

compiles just fine, but
For i = 1 to 10
Next i
generates an error when compiling.

Why this change? With nested loops, "Next i" is much easier to follow when reading code than multiple "Next" statements. Of course, a simple work around is
Next 'i
but it doesn't seem that should have to be necessary.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Why this change? With nested loops, "Next i" is much easier to follow when reading code than multiple "Next" statements.
I can't speak for why Erel disallowed it in Basic4android, although I suspect it is rather that it was a side-effect that it did work in Basic4ppc.

The "Next var" syntax is a hangover from early unstructured Basic dialects that didn't insist on strict block structuring so you could actually choose which loop variable to change. Modern structured Basics don't allow this, the variable is always implicitly defined so the syntax is no longer possible.

As for not declaring variables - There is a performance penalty to pay for not defining numeric variables as numeric types as they will be converted to and from Strings and Doubles as they are used. However there should be no other effect apart from maybe the minor loss of a couple of significant digits accuracy during prolonged calculations requiring many conversions.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Klaus recommends declaring all variables. That's a nice thought, but my program is math intensive and includes code translated from C, Fortran, BASIC
Especially for math programs it's the time woth, much better performance.
I have also some math intensive programs, and I declared all the variables in these programs when moving from B4PPC 6.8 to B4PPC 6.9.

Best regards.
 
Upvote 0

dlfallen

Active Member
Licensed User
Longtime User
Yes, I understand that explicitly defining variables results in a performance enhancement. Perhaps I misused the term "math intensive". What I meant was, there is a lot of math code, but there are no intensive computations.

I "grew up" on early versions of BASIC. I was an early adoptor of the IBM PC. It was a Model 1 which was only produced for 6 months. I had to write my own database software as there was none available. I did many, many benchmarks with declared/undeclared variables and on that slow processor it made a huge difference. Huge, but not enough as I had to resort to writing most computational routines in assember in order to attain adequate speed. But as the PC evolved, it got faster and faster. Today, my cell phone is a zillion times faster than my first PC.

It just doesn't take that long to compute a gamma function - with or without explicitly declared variables. My time savings comes on the developmental end, not on the processing end. I could do a turing test between my program without declared variables, and one with declarations, and I doubt one could tell the difference. Sure, some programs will show a noticible improvement and if I ever need the speed I'll certainly take the time to declare everything. In the meantime, I live by an old graduate school addage - A difference that doesn't make a difference is not a difference.

Klaus, Andrew: I really appreciate your advice and all the great help you both have provided over the last few years. May you live long and prosper!
 
Upvote 0

CidTek

Active Member
Licensed User
Longtime User
I am trying to loop through a List returned by an InputMultiList in reverse order.

size of list = 3

For i = result.size-1 to 0
Log(i)
Next

and it never enters the loop and logs any i values.

I have to loop through backwards so I can remove items off a stack without changing the upper order.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Hi,

You need to tell it to process backwards i.e.

For i = result.size-1 to 0 Step -1
Log(i)
Next

Otherwise the test will fail as result.size-1 would be > 0.

Steve
 
Upvote 0

CidTek

Active Member
Licensed User
Longtime User
Hi,

You need to tell it to process backwards i.e.

For i = result.size-1 to 0 Step -1
Log(i)
Next

Otherwise the test will fail as result.size-1 would be > 0.

Steve

Ah, I see. I thought steps were just for when increments were different than 1.

Thanks!
 
Upvote 0
Top