Today's CPUs are developing faster and faster. From 386 and 486 to today's Pentium and Pentium II, the development speed can be said to be very fast. However, with the rapid development of CPUs, the "weight" of application software has also skyrocketed. From a few K or dozens of K when I was learning computers to tens of megabytes or hundreds of megabytes today, this change has even affected me. All were surprised. The advancement of CPU has made people work faster and greatly increased efficiency. At the same time, it has also developed people's bad habit of "not seeking simplicity." The author once saw a friend writing a program using VBA. The program code is as follows:
WithActiveWindow
IfWindowState=wdWindowStateMaximizeThen'thisisthefirstpart!
MsgBox"ThisisaMaximizeWindow"
EndIf
IfWindowState=wdWindowStateMinimizeThen'thisisthesecondpart!
MsgBox"HereisaMinimizeWindow"
EndIf
IfWindowState=wdWindowStateNormalThen'thisisthethirdpart!
MsgBox"Nowhere,isaNormalWindow!"
EndIf
EndWith
At first glance, it seems that the structure is quite beautiful and it is a good program. But if you analyze it carefully, you will find that this procedure is a failure. Because these three programs do not implement the logical structure they should have. I told this friend about this opinion, and he listened very well and immediately modified the above program. The specific program code is as follows: WithActiveWindow
IfWindowState=wdWindowStateMaximizeThen'thisisthefirstpart!
MsgBox"ThisisaMaximizeWindow"
ElseIfWindowState=wdWindowStateMinimizeThen'thisisthesecondpart!
MsgBox"HereisaMinimizeWindow"
ElseIfWindowState=wdWindowStateNormalThen'thisisthethirdpart!
MsgBox"Nowhere,isaNormalWindow!"
EndIf
Endwith
After my friend finished writing this program, he smiled at me and said, "How is it?" I looked at this program and thought: If WindowState is not equal to wdWindowStateMaximize, then I have to judge whether WindowState is equal to wdWindowStateMaximize. wdWindowStateMinimize, what if it is not equal to yet? Then you have to continue to judge whether it is equal to wdWindowStateNormal. At this point, you have to break through three "doors" before you can enter. Are you tired? I got impatient and wrote a program myself:
SelectCaseWindowState
CasewdWindowStateMaximize'thisisthefirstpart!
MsgBox"ThisisaMaximizeWindow"
CasewdWindowStateMinimize'thisisthesecondpart!
MsgBox"HereisaMinimizeWindow"
CasewdWindowStateNormal'thisisthethirdpart!
MsgBox"Nowhere,isaNormalWindow!"
EndSelect
As you can see, this program is concise, easy to understand, and highly readable. I believe it will add a lot of color to the program. And if you debug it, you will find its other advantages. No wonder my friend suddenly realized it!
In fact, there are rules to follow as to which judgment statement to use. For example: if there are two possibilities, then you can choose If/Elseif without hesitation; for two or more possibilities, it is best to choose SelectCase/EndSelect. In most cases, if there is only one optional relationship, then "ifthen/endif" will be the best choice. ->