Share VBS For Next loop traps

Source: Internet
Author: User

When I wrote "QWERTY password: encryption and decryption" Yesterday, I wrote this For Next loop to get a string of 26 letters: Copy codeThe Code is as follows: 'author: Demon
'Website: http://demon.tw
'Date: 2012/2/10
For I = 65 To (I + 25)
S = s & Chr (I)
Next
WScript. Echo s

After running the program, I found no string output, and thought it was strange. So I simply modified it:Copy codeThe Code is as follows: 'author: Demon
'Website: http://demon.tw
'Date: 2012/2/10
For I = 65 To (I + 25)
WScript. Echo Chr (I)
S = s & Chr (I)
Next
WScript. Echo s

There is still no output, which means that the statements in the For Next loop are not executed at all and cannot be understood. So I consulted the prophet Evening News, and he soon found the trap:Copy codeThe Code is as follows: 'author: Demon
'Website: http://demon.tw
'Date: 2012/2/10
For I = 65 To (I + 25) Step-1
WScript. Echo Chr (I)
S = s & Chr (I)
Next
WScript. Echo s

Finally, I believe that you will find the trap. The order of values For the For Next loop is not from left to right. The expression (I + 25) has been evaluated before I = 65, at this time, the I value is the default 0, so the original cycle is equivalent:Copy codeThe Code is as follows: 'author: Demon
'Website: http://demon.tw
'Date: 2012/2/10
For I = 65 To 25
S = s & Chr (I)
Next
WScript. Echo s

Of course there will be no output. Finally, I changed the program to this:Copy codeThe Code is 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 is straightforward and will not encounter 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.