Partilhar via


Como: executar instruções dependendo de uma ou mais condições (Visual Basic)

You can use an Declaração If...Then... (Visual Basic) to run a specific statement or block of statements depending on the Tipo de dados booleanos (Visual Basic) value of a condition. The condition usually results from a comparison of two values, but it can be any expression that evaluates to a Boolean value (True or False). This includes values of other data types, such as numeric types, that have been converted to Boolean.

To run one or more statements if a condition is True

  • If you have only one statement to run, use the single-line syntax of the If...Then...Else construction. You do not need the Else or End If statements. The following example illustrates this.

    Sub fixDate()
        Dim myDate As Date = #2/13/1973#
        If myDate < Now Then myDate = Now
    End Sub
    

    - ou -

  • To execute more than one line of code when the condition is True, use the multiple-line syntax, which includes the End If statement. If you have no code to run when the condition is False, you omit the Else statement. The following example illustrates this.

    Dim alertLabel As New System.Windows.Forms.Label
    Sub alertUser(ByVal value As Long)
        If value = 0 Then
            alertLabel.ForeColor = System.Drawing.Color.Red
            alertLabel.Font = New Font(alertLabel.Font, 
                FontStyle.Bold Or FontStyle.Italic)
        End If
    End Sub
    

To run some statements if a condition is True and others if it is False

  • Use an If...Then...Else a construção com o Instrução Else (Visual Basic)dedemonstrativo para definir os dois blocos de instruções. Visual Basicexecuta um bloco se a condição for True e o outro se for False. The following example illustrates this.

    Dim alertLabel As New System.Windows.Forms.Label
    Sub alertUser(ByVal value As Long)
        If value = 0 Then
            alertLabel.ForeColor = System.Drawing.Color.Red
            alertLabel.Font = New Font(alertLabel.Font, 
                FontStyle.Bold Or FontStyle.Italic)
        Else
            alertLabel.Forecolor = System.Drawing.Color.Black
            alertLabel.Font = New Font(alertLabel.Font, 
                FontStyle.Regular)
        End If
    End Sub
    

To test additional conditions if the first condition is False

  • Use an If...Then...Else construction with one or more ElseIf statements to test additional conditions if the first condition is False. In the following example, the Function procedure computes a payroll bonus based on performance rating. The statement block following the Else statement runs only if the conditions in the If and ElseIf statements are all False.

    Function bonus(ByVal performance As Integer, 
                   ByVal salary As Decimal) As Decimal
        If performance = 1 Then
            Return salary * 0.1
        ElseIf performance = 2 Then
            Return salary * 0.09
        ElseIf performance = 3 Then
            Return salary * 0.07
        Else
            Return 0
        End If
    End Function
    

    Visual Basic tests the conditions in the order they appear in the If...Then...Else statements. When it encounters a True condition or an Else statement, it runs the corresponding statement block. Control then passes to the statement following the End If statement.

    You can have any number of ElseIf statements, or none at all. You can include or omit one Else statement regardless of whether you have any ElseIf statements.

Consulte também

Tarefas

Como: Transferir controle para fora de uma estrutura de controle (Visual Basic)

Como: Testar vários valores de uma expressão (Visual Basic)

Como: Manter controle quando ocorre um erro (Visual Basic)

Conceitos

Estruturas de decisão (Visual Basic)

Estruturas de loop (Visual Basic)

Outras estruturas de controle (Visual Basic)

Estruturas de controle aninhado (Visual Basic)

Outros recursos

Fluxo de controle no Visual Basic