Byte in Backward Loop

DocAElstein

Banned user
Joined
May 24, 2014
Messages
1,336
Hi,
. Sorry- probably an obvious answer. But Googling gave mostly only guesses.
. I prefer sometimes to limit things initially in size and so often dimension variables as Byte ( 0 - 255 ).
. I noticed something Weird.
. This works:
Code:
Sub TestByteloopForwards()
Dim Count As Byte
  For Count = 1 To 2 Step 1
  MsgBox "Hi Weld"
  Next Count
End Sub
. This does not:
Code:
Sub TestByteloopBackwards()
Dim Count As Byte
  For Count = 2 To 1 Step -1
  MsgBox "Hi Weld"
  Next Count
End Sub

. Obviously I can change to an Integer or even something bigger.
.. But I am learning, and would be interested to find out why
. Any ideas?

Thanks
Alan
 
A simplifying approach for now …., and not worry about the minutiae……..
Hi,
Yep, I fully agree a practical working understanding is enough (certainly for me) to get on with! (And anyway, I get the feeling nobody truly knows anymore the exact minutiae of wot a computer does in any simple process in VBA, and it is mostly down to experience…. And I’m grateful to you giving the benefit of yours as always!!)

…If I may just indulge a little further to that end… of a working understanding…

A simplifying approach for now would be to always use Longs for loop index variables,……That will get you a long way downstream.

. .I cannot unfortunately for now always use Longs for loop index variables as in my project (and in the threads I answer). There are often good reasons for defining my index variables in different types. But I think if I go with the simplified explanation…

In the For Loop…
… .. The TYPE of Start, Stop or Step variable will be seen to be of, or „redimensioned“ to, the type of the “bound” variable (my original “Count”), and remain so while the loop is running. This will be somehow set at the start of the For loop. These „set“ or ReDimensioned variables will be held in some unseen place purely for use in that loop. Any set Dimensioning we have remains in our program but is ignored by the loop.

. To demonstrate open both the immediate window and the watch window setting a watch on all the variables in the following code.
. Step through with F8 and observe that at no time are variable types changed as far as we can see. However the loop behaves as expected as if the TYPE of Start, Stop or Step variable are of changed to that of the type of the “bound” variable (my original “Count”).

Code:
Sub TestieDAE()
Dim BoundVariableCount As Byte
 
Dim StartValue As Double
Let StartValue = 1.1
Dim StepValue As Single
Let StepValue = 1.2
Dim EndValue As String
Let EndValue = "10"
 
  For BoundVariableCount = StartValue To EndValue Step StepValue
  Debug.Print BoundVariableCount & "  " & StartValue & "  " & StepValue & "  " & EndValue
  Let EndValue = "20"
  Let BoundVariableCount = BoundVariableCount + 1.1
  Let EndValue = "20"
  Next BoundVariableCount
 
  Debug.Print BoundVariableCount
  Debug.Print StartValue
  Debug.Print StepValue
  Debug.Print EndValue
 
 
 
End Sub

And as expected this next code will crash at the start of the loop with an overflow error, as a variable is outside the range of the bound variable. (The same argument goes if any value is –ve, to answer my very original question)

Code:
Sub TestDAE()
Dim BoundVariableCount As Byte
 
Dim StartValue As Double
Let StartValue = 1.1
Dim StepValue As Single
Let StepValue = 1.2
Dim EndValue As String
Let EndValue = "266"
 
  For BoundVariableCount = StartValue To EndValue Step StepValue
  Debug.Print BoundVariableCount & "  " & StartValue & "  " & StepValue & "  " & EndValue
  Let EndValue = "20"
  Let BoundVariableCount = BoundVariableCount + 1.1
  Let EndValue = "20"
  Next BoundVariableCount
 
  Debug.Print BoundVariableCount
  Debug.Print StartValue
  Debug.Print StepValue
  Debug.Print EndValue
 
 
 
End Sub

. As for the other bit about coercing, converting, “capable of being interpreted as a number“ etc..
Maybe that is not so far away from a simple explanation now:

. The following code fails again at the start of the Loop but with a different error that do with type compatibility
(because “10a” cannot be changed / coerced or whatever to 10 by the loop)

Code:
Sub TesDAE()
Dim BoundVariableCount As Byte
 
Dim StartValue As Double
Let StartValue = 1.1
Dim StepValue As Single
Let StepValue = 1.2
Dim EndValue As String
Let EndValue = "10a"
  For BoundVariableCount = StartValue To EndValue Step StepValue
  Debug.Print BoundVariableCount & "  " & StartValue & "  " & StepValue & "  " & EndValue
  Let EndValue = "20"
  Let BoundVariableCount = BoundVariableCount + 1.1
  Let EndValue = "20"
  Next BoundVariableCount
 
  Debug.Print BoundVariableCount
  Debug.Print StartValue
  Debug.Print StepValue
  Debug.Print EndValue
 
 
 
End Sub

. As for the minutiae of what happens first…(The “two stage process” ). - I do not think ( for now!) that is easy to determine. If you experiment a bit with that code and make note of the errors, I think one finds that the: a) compatibility check of whether the variable lies in the range of the bound variable or ; b) being able to be changed to a double, appear to take place at the same time but in the order as written at the outset of the code. That is to say in the order of
StartValue EndValue StepValue

. Alan
 
Upvote 0

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
...and from the huh? rookie corner... from what I have (sort of) grasped... use Longs for whole numbers, period*.

I seem to recall that in an earlier post, you mentioned 'saving' memory and using the smallest bit. But upon/after 32-bit, by all I have read, Integers (Short) need converted internally anyways, so a Long is actually faster.

Mark

* disclaimer - certain APIs will require a certain bit structure...
 
Upvote 0
That's correct. Unless you need some sort of validation (i.e. the value should only be within the range of a Byte/Integer) there's no point using anything but Long.
 
Upvote 0
.......
I seem to recall that in an earlier post, you mentioned 'saving' memory and using the smallest bit. But upon/after 32-bit, by all I have read, Integers (Short) need converted internally anyways, so a Long is actually faster.
......

Thanks, that is a useful addition. I will remember that. So I will only use Long when I am intertested in an integer number, unless I have a particular reason other than to do with memory space to limit the maximun number size. (Although I could use other methods I guess for that so maybe the
use Longs for whole numbers, period
is a good idea!
(But for the other variables I Dimension as before as needed or as required by other parts of the code)
. We are getting there!:)
Alan
P.s. I note that applies when I am interested in INTEGER Numbers. Of course the bound variable may be defined as Double(or Single) if I am for example iterested in fractional increments in the stepValue in the Loop
 
Upvote 0
We were talking about whole numbers, I believe. ;)
 
Upvote 0
Signing off this thread ...


. Thanks to participating in this Thread I really have a good understanding of the whole working of the For loop now (inside and out!). I am very grateful for your help and patients. And many thanks also to Jerry, Mark and RoryA for their helpful contributions





Alan Elston , signing out!
 
Upvote 0
Hi,
. A few minor extra points seen by experimenting and again a summary for future viewers of this thread

. .. I noticed that increment or “Step Value” used in the loop for it’s Loop workings cannot be changed within the loop, as previously noticed this was also the case for the End Value used in the Loop for it’s Loop workings, ( http://www.mrexcel.com/forum/excel-...-do-while-loop-end-condition-can-changed.html ). Presumably that is also the case for the Start Value, but that is difficult to check experimentally. Also note as discussed in (http://www.mrexcel.com/forum/excel-...-do-while-loop-end-condition-can-changed.html ) and further in this thread, that the Bound Variable (Count) can be changed in the loop, although there are good reasons to avoid this.

. .. I noticed that if the Bound Variable is not Dimensioned, then at the start of the loop it will be dimensioned to “suit” the Dimensioning of the Loop variables: Start Value; End Value; and Step Value. Specifically if any of these is a Double, then the Bound Variable ( Count ) will, at outset the loop be Dimensioned as a Double. It then sort of “works down” the Dimensioning.. That is to say it looks downwards from Double, then Single (Unless in addition to any loop variable being Single , any other is Long: Then it will be dimensioned as Double!!), then Long, then Integer, then lastly Byte.

……………..
. So it all sort of resolves around the Bound Variable (Count). He is the “Boss” in the loop and forces the Loop Variables (​Start Value; End Value; and Step Value) as he sees them for use in the Loop working to be of his type regardless of their actual types. (This is done at the outset of the loop). The actual types of the Loop Variables are retained by themselves before, in and after the loop. They are not affected by the loop, so the values seen and used by the loop in It’s working as a loop are held in some other place and not where the actual Loop Variables are stored. Although he is the “Boss”, he allows his value (not type) to be varied in the loop and, if he is not specifically Dimensioned, then he will Dimension himself to suit, as it were, The “Biggest” Dimensioned Loop Variable. (This is also done at the start of the Loop). But note I am not using “Biggest” literally, - I sort of mean the most complicated Dimension, going in the order: Double; (Long + Single = Double); Single; Long; Integer; Byte.

. I hope that helps. I can see why Microsoft were not able to clearly explain wot at first seems a fairly simple Thing.

. Maybe if I get time I will try to give a more clear all encompassing definition / explanation of the working of the For / Next loop. I (think?) I have it all clear now (at least in my head!?).

Alan
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,342
Messages
6,130,114
Members
449,558
Latest member
andyamcconnell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top