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
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Reckon because Byte is an unsigned integer, so there's no representation of -1
 
Upvote 0
I am pretty shaky/sketchy when it gets to things that are this near to the 'core' if-you-will, of how the computer actually works at binary string level, so I hope you get a better answer than this:

As you know, the Byte Data Type is an unsigned 8-bit, which as Help puts it, results in... "stored as single, unsigned, 8-bit (1-byte) numbers". As stated, I have an overly basic understanding of this, but I do not believe Byte is really supplied (designed) for adding or subtracting in the common meaning (Scalar). Of course you can use it as long as there are no values out of its range, like:

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> BytePlusMinus()<br><SPAN style="color:#00007F">Dim</SPAN> Count <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>   <SPAN style="color:#007F00">'00000000</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> bits() <SPAN style="color:#00007F">As</SPAN> Byte<br>  <br>  Count = 1         <SPAN style="color:#007F00">'00000001</SPAN><br>  Count = Count + 1 <SPAN style="color:#007F00">'00000010</SPAN><br>  Count = Count - 1 <SPAN style="color:#007F00">'00000001</SPAN><br>  Count = Count - 1 <SPAN style="color:#007F00">'00000000</SPAN><br>  Count = Count + 7 <SPAN style="color:#007F00">'00000111</SPAN><br>  <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>  Count = Count - 8 <SPAN style="color:#007F00">'Overflow error / no sign bit to handle the negative</SPAN><br>  <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>  <br>  bits = "A"<br>  <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>

Now when you step thru that with the Locals window open, of course it goes fine until ...-8, where an Overflow error occurs. But note that bits = "A" results in a Byte array of 2 elements, values of 65 and 0. I am pretty sure this is more at the reason Byte was supplied, and not for scalar arithmetic. I would use Long.

Oh, and I am not sure why the dropping value For...Loop immediately errors, but would suspect that it is the way the 'value', that is, the binary string, is being evaluated.

Well, probably not of much help, but I hope we get a more articulate explanation.

Mark
 
Upvote 0
I am pretty shaky/sketchy when it gets to things that are this near to the 'core' if-you-will, of how the computer actually works at binary string level, so I hope you get a better answer than th......................... but I hope we get a more articulate explanation.

Mark

Thanks Mark.
. Not such an obvious question/ answer after all. As I said in Google & co. were only guesses.
. See wot else comes up for an explanation.
Alan
 
Upvote 0
. So the Step variable is taken as the same type as the Count variable??

Alan, Microsoft's VBA Language Specification generally describes the For Statement algorithm in "5.4.2.3 For Statement"; however aside from the requirement that the Step Increment be "Let-coercible to Double" the document doesn't address the issue of whether its range is limited to that of the "bound-variable-expression" (the variable "Count" in your examples).

In doing a quick test, it appears that the value of the Step Increment cannot fall outside the range of the data type of the "bound-variable-expression"

Try running these two subs....

Code:
Sub TestIntegerBackwards1()
'--Integer data types can store integer numbers in range of -32,768 to 32,767
'  This code will print values 32000 To -32000 then throw an overflow error when it 
'     attempts to increment i to -33000 before testing if -33000 is greater 
'     than the end-value expression -32000 

 Dim i As Integer
 For i = 32000 To -32000 Step -1000
   Debug.Print "i= " & i
 Next i
End Sub

Code:
Sub TestIntegerBackwards2()
'  This code will throw an overflow error before printing any values of i
'  even though it should be able to print 32000 and -1000 before the incremented
'  value attempts to reach -33000 where TestIntegerBackwards1 failed.

 Dim i As Integer
 For i = 32000 To -32000 Step -33000
   Debug.Print "i= " & i
 Next i
End Sub
 
Upvote 0
Hi mark,
I am pretty shaky/sketchy when it gets to things that are this near to the 'core' if-you-will, of how the computer actually works at……



**bits = "A"
**
End Sub


Mark

. Thanks again mark. I played around with your code.. The bis="A" was interesting.. Hadn’t seen anything like that before..

…… but I hope we get a more articulate explanation. …

… looks like we have a fairly good answer now (At least probably as good as anyone knows!!!)

Alan.
 
Upvote 0
Hi Jerry,
…… Microsoft's VBA Language Specification generally describes the For Statement algorithm in …….
….thanks, useful link my Googloing did not catch.

……
Try running these two subs....

. Useful demo thanks.



…….. however aside from the requirement that the Step Increment be "Let-coercible to Double" the document doesn't address the issue of whether its range is limited to that of the "bound-variable-expression" (the variable "Count" in your examples).

In doing a quick test, it appears that the value of the Step Increment cannot fall outside the range of the data type of the "bound-variable-expression" ………

. So looks like by trial and error we have maybe the answer(s):
. „the value of the Step Increment cannot fall outside the range of the data type of the "bound-variable-expression"


. Surprises me that in computing one has to find things out that way. I guess the stuff is just so big and complicated that even the original people who wrote all the software have forgotten or lost track!!


. A second answer then could be noting the bit
…… the requirement that the Step Increment be "Let-coercible to Double" the document doesn't addre……….]
could be

. It is bad computing practice to use anything other than Long for the Count variable!

. Reminds me of the answer I got to a similar question about the For / Next Loop. ( http://www.mrexcel.com/forum/excel-...-do-while-loop-end-condition-can-changed.html ) … -I Noticed that You could change the Count variable but not the End value from within the loop. I got no specific links to a written answer. (And I do not see it at first glance in the link You gave me..)… so the general answer seemed to be along the lines …

. It is bad practice to change them in the loop!

. Thanks again for the reply.
Alan Elston
 
Upvote 0
Regarding why you shouldn't change the loop index variable inside a loop:

A practical reason is that it can make debugging rediculously difficult.

A computer science reason is that the loop index variable is a bound variable, which means it has no context outside outside the operator that binds it. So, for example, in this expression (which should be in sigma notation)

Sum of 1/i for i =1 to 10

... there is no outside value of i that influences the calculation; it is bound by the summation operator. The For statement does (is intended to do) the same thing; in fact, in the language spec, the loop index variable is referred to as < bound-variable-expression > (sans spaces). Changing the variable inside the loop breaks the definition; it is no longer a bound variable.
 
Upvote 0
Regarding why you shouldn't change the loop index variable ins.......
.........


. . I guess there are always very good reasons that a beginner like me overlooks.
. Many thanks, as always for the contribution and for giving us the benefit of your experience and Knowledge. It is always very much appreciated.
Alan.

.
 
Upvote 0

Forum statistics

Threads
1,214,817
Messages
6,121,720
Members
449,050
Latest member
MiguekHeka

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