Some details of VBS for next loop

Time:2022-5-21

For NextThe basic structure of the statement is:

Copy codeThe code is as follows:


For counter = start To end [Step step]
[statements]
[Exit For]
[statements]
Next


When for nextloopAt the beginning, Visual Basic Scripting Edition(VBSScript) assign start to counter. Before running the statement block in the loop, compare counter with end. If the counter has exceeded the value of end, the for loop terminates and the control flow jumps to the statement after next. Otherwise, run the statement block in the loop.

Every time VBScript encounters next, it will add step to counter and return to for. It compares the values of counter and end again, and continues to run the statement block in the loop or terminates the loop according to the result. This process will continue until the counter exceeds end or an exit for statement is encountered.

The above is common sense. Simply mention the word count. Here is a detail you may ignore:

The loop control variables start, end and step are evaluated only once before the loop starts. If you change the value of end or step in the statement block of the loop, this change will not affect the operation of the loop.

Write a simple vbs script to verify:

Copy codeThe code is as follows:


‘Author: Demon
‘Date: 2012-1-19
n = 10
s = 1
For i = 1 To n Step s
WScript.Echo i
n = 5
s = 2
Next
WScript.Echo n, s


We changed the values of N and s in the loop, but the loop still goes on for 10 times and outputs 1 to 10 in turn, indicating that a copy of the loop control variables N and S is saved in the for next loop, and this internal variable is used to control the flow of the loop.

Understanding this detail will help us write efficient and concise code:

Copy codeThe code is as follows:


‘Author: Demon
‘Date: 2012-1-19
s = “http://jb51.net”
For i = 1 To Len(s)
WScript.Echo Mid(s, i, 1)
Next

‘This way of writing C style is not more efficient than the above
‘you don’t need to do this in the for next loop of VBS
l = Len(s)
For i = 1 To l
WScript.Echo Mid(s, i, 1)
Next


It’s worth noting that changing the counter in the loop is allowed, but you should avoid doing so, which will only make your script difficult to read and debug.

Copy codeThe code is as follows:


‘Author: Demon
‘Date: 2012-1-19
For i = 1 To 10
WScript.Echo i
I = I + 2 ‘is not recommended
Next

 

Reference link:http://msdn.microsoft.com/en-us/library/sa3hh43e%28v=vs.85%29.aspx

Original text: http://demon.tw/programming/vbs-for-next.html

Recommended Today

Fastems.Mms.Client.BaseData.Service.BaseDataService之FixtureSummaries

in Fastems.Common.dll,   Fastems.Mms.Client.BaseData.Views, In Fixtures/Views/FixtureLibraryView.xaml, the data source of Fixtures has been unclear. The records are as follows. The data source of the View is the Fixtures in Fastems.Mms.Client.Fixtures.ViewModel.FixtureLibraryViewModel, And the source of Fixtures is Fixtures = new PagingCollection(_fixtures, FixtureFilterSources);   BatchingObservableCollection _fixtures; _fixtures creates a collection object BatchingObservableCollection in the constructor public class BatchingObservableCollection : […]