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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Personally I think the specification covers it implicitly when it says:
The declared type of < start-value >, < end-value >, and < step-increment > must be statically Let-coercible to Double

Note that it says 'type' not 'types'.

I think there's a two stage process in that all values are coerced to the declared variable type first, then to Double, which explains the infinite loop if you run this:
Code:
Sub foobar()
    Dim n As Long
    For n = 0.6 To 1.2 Step 0.1
        Debug.Print n
        DoEvents
    Next n
End Sub
because the step value is actually 0.
 
Upvote 0
all values are coerced to the declared variable type first

Going back to my first post, I agree with you that that happens, but I don't see how you read that into the language. It seems like they should have written,

The declared type of < start-value >, < end-value >, and < step-increment > must be statically Let-coercible to the data type of the bound variable
 
Upvote 0
The fact it says type, not types, implies they must all be treated as the same type, and given that start and end must be assigned to the bound variable, it follows they must be coerced to that same type, I think. That type in turn must be coercible to a Double, which is really the key to the loop working.

I completely agree that the wording is lacking though (which is true of a fair few parts of the specification in my opinion).
 
Upvote 0
The fact it says type, not types, implies they must all be treated as the same type, and given that start and end must be assigned to the bound variable, it follows they must be coerced to that same type, I think. That type in turn must be coercible to a Double, which is really the key to the loop working.

I completely agree that the wording is lacking though (which is true of a fair few parts of the specification in my opinion).

Danke for reply :)
 
Upvote 0
@Rory:

...That type in turn must be coercible to a Double, which is really the key to the loop working.

Only if you are really really bored and feel like it... I am not in the least grasping why it would need to be a Double to work in a loop.

@shg, Rory, and Jerry:

Thank you all very much :-) It is always nice to understand a particular thing just a tad better. Now if I could just get the 'memory leak' in my brain plugged up...

Mark
 
Upvote 0
@Rory:



Only if you are really really bored and feel like it... I am not in the least grasping why it would need to be a Double to work in a loop.

@shg, Rory, and Jerry:

Thank you all very much :-) It is always nice to understand a particular thing just a tad better. Now if I could just get the 'memory leak' in my brain plugged up...

Mark


On second reading I am not quite with it either. Jerry's answer I followed. The infinite loop demo and discussions thereafter I do not quite follow (yet). Maybe the Penny will drop when I read it afew more times!
Alan
 
Upvote 0
Rory said:
The fact it says type, not types, implies ...
Ah, gotcha.

@ Doc:

In the loop, < start-value > is initialized to CLng(0.6), which is 1, < end-value > is set to CLng(1.2), which is 1, and < step-increment > is set to Clng(0.2), which is 0.

You might think that the loop should execute once and quit, but the exit test is that the bound variable is greater than the end value after being incremented at the bottom of the loop, and that doesn't happen. Rory made a very good example.

And the DoEvents was a kindness to avoid the need to kill Excel to get it to stop.
 
Last edited:
Upvote 0
......

In the loop, < start-value > is initialized to CLng(0.6), which is 1, < end-value > is set to CLng(1.2), which is 1, and < step-increment > is set to Clng(0.2), which is 0.

You might think that the loop should execute once and quit, but the exit test is that the bound variable is greater than the end value after being incremented at the bottom of the loop, and that doesn't happen. Rory made a very good example.

And the DoEvents was a kindness to avoid the need to kill Excel to get it to stop.

. Thanks,
I get it now, (I thought Long accepted less than 1 and converted 0.6 to something like 6 x 10 to the power of -1):stickouttounge:
. Alan
 
Upvote 0
. Thanks,
I get it now, (I thought Long accepted less than 1 and converted 0.6 to something like 6 x 10 to the power of -1):stickouttounge:
. Alan


... I think I mixed up Long with Double, as I do not usualy use either of them very often. ( I mainly sort of sort things with VBA and do not do calculations very often ).
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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