Learning VB programming language, some people think it is very difficult, some people think it is particularly easy, a learning will, simply extremely, you also have this feeling. Let me give you some answers.
The control process of computer execution can only consist of three kinds of basic control structure, namely: sequential structure, selection structure and cyclic structure. Visual Basic is an event-driven process by which users fire events to perform the corresponding events. These three basic structures are always included within each event processing. Learning VB programming language is really simple, learn to go around the world's three VB control structure, you do not have to learn VB to worry about, absolutely really achieve a learning will.
We will first introduce the concept of three structures and syntax format, and finally explain the advantages and disadvantages of each structure.
First, sequential structure
Sequential structure: is executed according to the written order of the statement (how the statement executes, how to write). A few simple examples:
1. Assignment statement let (the most basic and most commonly used statement in program design)
Assignment Statement General format: [let] < variable name >=< expression >;[let] [< object name;.] < property names >=< expressions >
Function: Assigns the value of an expression to a variable or sets the property of an object.
2. Data output
Let's introduce the Information box function MsgBox
The Msgbox function is formatted with the:< variable name >=msgbox (< hint info >[,< dialog box type >[,< dialog title;])
Its role: pop-up messages to display information.
3. Data entry
Input Box function InputBox
Syntax format: variable name =inputbox (< message >,[< Dialog caption >],[< default);])
What it does: Returns the data entered by the user to the current program.
4. Common statements
1. Unload Object Statement (Unload)
Syntax format: Unload object name
Role: Uninstalls the specified form or control.
2. Closing sentence (END)
Syntax format: End
Function: Forcibly terminates the program and clears all variables.
Above we say so much, the purpose is like to tell you, the order structure to follow the syntax format, syntax format is wrong, the system will error.
II. Choice of structure
1. Conditional statements (single and multiple lines only)
(1) Single-line conditional sentence
Syntax format:
If < conditions > then < statement block 1> [Else < statement block 2>]
(2) Multi-line conditional sentence (block structure condition statement)
Syntax format:
Syntax format: If < conditions > then
< statement block 1>
[Else
< statement block 2>
End If]
(3) Nesting of conditional statements
① Conditional statement nesting in general format: write a conditional statement in a conditional statement.
Format:
Conditional statement nesting in ②ELSEIF format: Solving multi-layered conditional sentences makes writing and reading programs difficult.
Format:
If < condition 1> then
< statement block 1>
ElseIf < condition 2> then
< statement block 2>
ElseIf < condition 3> then
< statement block 3> ...
[Else
< statement block N>]
End If
(4) Use the IIf function: To achieve a relatively simple conditional judgment sentence.
Format:
IIF (< conditional expression >,< True Value >,< condition false value >)
2.Select Case Statement
Syntax format:
Select case< Test expression >
case< Test expression 1>
< statement block 1>
[case< test expression 2>
< statement block 2 ]
...
.... [case< Test Expression n>
< statement block N>] [Case Else
< statement block N+1>]
End Select
compared with the IF statement and the Select Case statement, when solving complex problems, if statement nesting is cumbersome, and the implementation of multi-branch selection, the structure is not obvious, select Case statement to achieve multi-branch selection, simpler, easier to read.
Third, the cycle structure
1.Do ... Loop statement (determines loop by detecting loop condition)
(1) Judge the condition first
Syntax format:
Do [while| Until < conditions;]
[loop body]
loop
(2) After judging conditions
Syntax format:
Loop
[loop body]
do [while| Until < conditions;]
Compared with the above syntax format is not difficult to find, they basically no difference, the condition is the first to judge the condition, the condition is the next judge condition.
2.For ... Next Statement (count loop, program structure known for cycle times)
Syntax format:
For < loop variables >=< initial values > to < final value > [step< step;]
[< loop body;]
Next [< loop variable;]
Do ... Loop statement with for ... The difference between next statements:
Example: Calculate 1+2+3+...+100.
not hard to find, do ... Loop statement than for ... The next statement has only one line, the biggest difference being the essence.
3. Multiple loops
As long as the above loops have learned, multiple loops are just a name, plus a loop in a loop.
Advantages and Disadvantages
Sequential structures can only solve some simple problems, which can be cumbersome to solve in relation to complex problems.
The selection structure can make different choices depending on the situation.
The loop structure repeats some statements and simplifies the program, which can improve efficiency.