VB.Net's For Next - vb.net

Is there a difference between these?
For i = 0 To Something.Length - 1
'do something
Next
For i = 0 To Something.Length - 1
'do something
Next i

It is only for readability:
You can optionally specify counter in the Next statement. This improves the readability of your program, especially if you have nested For loops. You must specify the same variable as the one that appears in the corresponding For statement.
From http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx

Nope. There is no difference. Even with nested loops there is no difference because nested for-loops cannot overlap.

Related

VBA - Leave Do Until when specific row is reached

In my VBA Code i go from the last row in my table to the top of my table. But i want that the Do Until Loop ends when it reached Row 10.
Right now I am using this Do Until Loop:
Do
' Do Something
Loop Until ActiveCell.Address = "$N$10:$BI$10"
How do i have to change my code that it will stop when it reached row 10?
Assuming rest of code is correct use:
Loop Until ActiveCell.Row = 10
If that fails you problem is logic in other parts of your code ie. ActiveCell never reaches row 10.
To answer your question in more theoretical fashion. There are multiple ways of exiting Do loops in VBA.
The proper way
1.1. While or Until at the begining
This checks the condition first, if the condition is met, it enters the loop and repeat with the condition being met at the start of every loop.
Do While i <= 5
'#code here
Loop
These two are equivalent.
Do Until i > 5
'#code here
Loop
1.2. While or until at the end
This is almost the same as what is described above. The only difference being, with sole Do statement at the beginning, your code-block always gets executed at least once! This can be particularly useful, when you want to execute something at least once, but don't want it to repeat unless a condition is met.
Do
'#code here
Loop While i <= 5
or
Do
'#code here
Loop Until i > 5
The enforced way
You can exit out of any loop, including Do with the so called Exit statement. This escapes the currently ongoing Do loop upon reaching the statement no questions asked. While you usually should try to avoid using the Exit statement, as in majority of cases it is possible to avoid using it with a proper condition at the While or Until portion of your code, it can come in handy in some cases.
Additionally, keep in mind, inside nested Do loops, Exit always exits only the innermost loop. This means, this would exit only the loop inside and let the others run, acting as a weird form of Continue
Do While (handler = True)
Do
'# execute me
If weird_condition = True Then
Exit Do
' i return to the "handler" loop
End If
field = field + 1
Loop Until field = field_amount
Loop
The not so nice enforced way
Alternatively, you can stop the entire exution, with the Stop statement. I would strongly advise against doing this, but technically it is a possibility so I'm listing it here. Similarly like End it ends the execution, but unlike End (eg. End Sub), it does not close any files or clear any variables - so technically this means you could use it to exit a loop. I would however recommend simply using the Exit statement instead. Can't really think of a case when I would ever use this.

Is there an equivalent of Python's pass statement in VBA?

I would like to know if there is an equivalent of Python's pass statement in VBA.
I am using Excel 2016.
The use of Stop (see this answer) seems to be the best thing to do if you are looking for some "non-statement" that you can use to insert a breakpoint, because the Stop command causes the code to break when it is reached, i.e. you don't even need to mark it as a breakpoint because it is one.
You might also like to consider using Debug.Assert some_logical_expression, which will break automatically whenever the logical expression evaluates to False. So Debug.Assert False would be equivalent to Stop, and Debug.Assert x = 3 would be equivalent to If x <> 3 Then Stop.
In Python you need the Pass, because otherwise the methods will not run.
In VBA, its perfectly ok if you leave an empty method like this:
Public Function Foo() As String()
End Function
Maby you are looking for the "Stop" statement.
The good thing about it is that it doesn't clear your variables.
It depends what are you trying to achieve.
You may declare a Label and then use GoTo Label e.g. declare a label (like Skip:)in your code where you want to jump if a condition is met and then use GoTo Skip
Below is the small demo code to give you an idea about this...
Dim i As Long
For i = 1 To 10
If i = 5 Then GoTo Skip
MsgBox i
Next i
Skip:

VBA: Why do people include the variable's name in a "Next" statement?

I have always written my For-loops like this:
For foo = 1 to 10
' do something
Next
However, when I read code snippets online, people always do this:
For foo = 1 to 10
' do something
Next foo
I have not noticed any difference between the two, and I can't find any documentation on next statement is more desirable. What is the difference between those two (if any)?
The counter after the Next statement is optional. It used to be required in BASIC-derived languages, but this is no longer the case in VBA.
You can check the VBA reference:
If you omit counter in a Next statement, execution continues as if counter is included. If a Next statement is encountered before its corresponding For statement, an error occurs.
The reason people still add the counter it to increase readability.
It's for when you have multiple for loops.
For example,
For i to j
For k to l
next k
next i
Otherwise, the next is ambiguous. It's not absolutely necessary, as the loop will still work without it, but it's just good practice to have it marked for the sake of anyone else reading your code.

VBA: Break Out of Deeply Nested If Statements

Typically when I want to break out of a statement I just set a boolean flag for control flow, but I have a special case with many nested If statements and I'd really like to have a way to break out of several with one simple statement.
In Java you can name a loop and then break to that location; is there anything like that for VBA that can be used from a deeply nested location in If statements? I know VBA has the Exit statement for loops (while, for, etc), so I'm wondering if there is something similar for Ifs.
Ideally I'd like to do something this:
If ...
*NAMED_IF*
If ...
If ...
:
*break out of NAMED_IF*
:
End If
End If
*Now We end up at this control position*
End If
There isn't an if-statement specific method to break out of nested if-statements, but you could use the GoTo-statement instead:
If ...
'*NAMED_IF*
If ...
If ...
'*break out of NAMED_IF*'
GoTo GoToHere
End If
End If
End If
'*Now We end up at this control position*
GoToHere:

Is there a way to use a nonlinear stepsize in a vb.net loop?

I would like to do the following c statement in vb.
for(int i = 2^20; i > 0; i/=2)
{
printf("%d\n",i);
}
In vb would look similar to:
For i As Integer = 2^32 to 0 Step /2
Console.Out.Writeline("{0}", i)
Next
Specifically, the variable where i is divided by 2 each iteration is not
legal vb.
Is there a way to write this using a For statement that is allowed?
No, the FOR loop in VB is shorter but less flexible.
And the obvious work-around is of course a While loop.
' untested
Dim i As Integer = 2^30 ' 2^32 will overflow
While i >= 0
Console.Writeline("{0}", i)
i = i Div 2
End While
There are two typical approaches:
Derive a formula to convert a linear (preferably integer-value) index into whatever values are needed for the loop, and then employ such a formula.
Use a method to generate one or more "IEnumerable"s to return the proper values in sequence and then replace the "For" with a "For Each"
The former approach is the one I usually use; I believe LINQ includes some methods to facilitate the latter approach (something like Enumerable.Range) but I don't know the details.