The trap sharing _vbs of the VBS for Next loop

Source: Internet
Author: User
Tags chr
When I wrote "QWERTY password: Encryption and decryption" Yesterday, I wrote this for Next loop in order to get a 26-letter string:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' DATE:2012/2/10
For i = (i + 25)
s = S & Chr (i)
Next
WScript.Echo S

After running but found that there is no string output, it is very strange, so a simple change:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' DATE:2012/2/10
For i = (i + 25)
WScript.Echo Chr (i)
s = S & Chr (i)
Next
WScript.Echo S

There is still no output indicating that the statement in the for Next loop was not executed at all, and puzzled, and consulted the evening prophet, who soon found the trap:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' DATE:2012/2/10
For i = Step-1 (i +)
WScript.Echo Chr (i)
s = S & Chr (i)
Next
WScript.Echo S

This time the species finally has the output, believe that smart you must have found the trap where. The order of the For Next loop is not evaluated from left to right, and the expression (i + 25) is evaluated before i = 65, and the value of I is the default of 0, so the original loop is equivalent to:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' DATE:2012/2/10
For i = 25
s = S & Chr (i)
Next
WScript.Echo S

Of course there is no output, and finally I changed the program to this:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' DATE:2012/2/10
For i = ASC ("A") to ASC ("Z")
s = S & Chr (i)
Next
WScript.Echo S

It's straightforward and does not run into the for Next trap.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.