Home > Software engineering >  About the If... Then... Elseif... Someone else and If statements nested can illustrate it to me? A n
About the If... Then... Elseif... Someone else and If statements nested can illustrate it to me? A n

Time:09-30

About the If... Then... Elseif... Someone else and If statements nested can illustrate it to me? A novice for help

CodePudding user response:

Use the select case:

Select a case a
In case a=1
.
In case a=2
.
In case a=3
.
In case the else
.
End the select

CodePudding user response:


The If... Then... The Else statement


According to the value of the expression conditionally execute a set of statements,

Syntax

If condition Then [statements] [Else elsestatements]

Or, you can use the block forms of syntax:

If condition Then
[statements]

[ElseIf condition - n Then
[elseifstatements]...

[the Else
[elsestatements]]

End the If

The If... Then... The Else statement syntax has the following several parts:

Part of the description
Necessary condition parameters, one or more has the following two types of expressions:
Numerical expression or string expression, and its result is True or False, if the condition is Null, the condition will be regarded as False,
TypeOf objectname Is objecttype forms of expression, the objectname Is any object references, and the objecttype Is any valid object type, if the objectname Is objecttype specified by an object type, the expression Is True, otherwise Is False,
Statements in the form of block is optional parameters; But in a single line, and there is no Else clause, it is necessary parameters, one or more statements separated by a colon, they perform when condition is True,
Condition - n optional parameters, with condition,
Elseifstatements optional parameters, one or more statements, they are related to carrying out for the True condition - n,
Elsestatements optional parameters, one or more statements, they are in front of the condition or condition - n are not True,


Description

Grammar) can use a single line (the first one to do a short and simple test, but the block form (the second syntax) provides more structured and adaptability, and is usually fairly easy to read, maintenance and debugging,

Pay attention to in a single line, in accordance with the If... Then judge the results can also perform multiple statements, all statements must be on the same line and separated by a colon, as shown in the following statement:

If A & gt; 10 Then A=A + 1: B=B + A: C=C + B

In block form, the If statement must be the first line statements, including the Else, ElseIf, and End the If part can only add line number or label at before, If block must be an End End of the If statement,

To decide whether a statement is an If block, can check Then what keywords after, If after Then the same line, there are other than the content of the comments, Then this statement is a single line If statements,

Else are optional, and ElseIf clause in the If block, can put any number of ElseIf clause, but must be before the Else clause, If block can also be nested,

When the program is run to an If block (the second syntax), the condition will be tested, If the condition is True, after Then the statement can be executed, If the condition is False, is part of every ElseIf conditions (If any) will, in turn, are calculated and tested, If find a condition is True, it just in the relevant statements will be performed after Then, If you don't have a ElseIf conditions to True (or no ElseIf clause), the program will execute the Else part of the statement, and the execution of the statement after Then or Else, will continue execution after End the If statements,

Hint according to the single expression to perform a wide range of possible action, the Select Case more useful, however, TypeOf objectname Is objecttype clause cannot be used in the Select a Case statement,

Note TypeOf does not like the Long, the Integer and the other is not the Object of fixed data types are used together,
 the If... Then... Else statements example 
This example demonstrating the If... Then... Else statements of two formats: "block format" and "single format", also demonstrated the If TypeOf... Then... Else,

Dim Number, who, MyString
Number=53 'initial value setting variables,
If the Number & lt; 10 Then
Who=1
ElseIf Number & lt; Then 100
'if the judgment is True, then complete the next line statements,
Who=2
The Else
Who=3
End the If

'the use of "single format" syntax to set a variable's value,
If who=1 Then MyString="One" the Else MyString="wining One '

Using the If TypeOf can determine whether to process control is a text box,

Sub ControlProcessor MyControl (As Control)
If TypeOf MyControl Is CommandButton Then
The Debug. Print "You passed in a" & amp; TypeName (MyControl)
ElseIf TypeOf MyControl Is CheckBox Then
The Debug. Print "You passed in a" & amp; TypeName (MyControl)
ElseIf TypeOf MyControl Is TextBox Then
The Debug. Print "You passed in a" & amp; TypeName (MyControl)
End the If
End Sub

  • Related