Conditional logic is the process of making decisions based on user input. This is a vital part of programming to understand. In fact, some may even argue that Conditional logic is all that programming is. Conditional logic always has some Boolean expression that can come to two possible conclusions: True or False. Your code then executes based on the value of this Boolean expression.

Before we discuss conditional logic at it's full potential, you must first understand the operators to use. Here are some of the operators in Visual Basic:

  • = - Equals
  • <> - Does not equal
  • < - Is less than
  • > - Is greater than
  • <= - Is less than or equal to
  • >= - Is greater than or equal to
  • And - Used to allow two or more conditions, where both must be True
  • AndAlso - Used to allow two or more conditions, where both must be True. This one does not check the second condition if the first is false. This is helpful if the second condition may have an error due to the fact that the first one is false.
  • Or - Used to allow two or more conditions, where either can be True
  • OrElse - Used to allow two or more conditions, where either can be True. This one does not check the second condition if the first is true. This is helpful if the second condition may have an error due to the fact that the first one is true.

NOTE: You can use these operators for both numbers AND strings. Numbers reference the values (greater than, less than), while strings go by alphabetical order, with A being the least and Z being the most.

Once you understand these, we can learn about the greatest Conditional logic there is. The "If" statement is probably the most used statement in coding to date. It has a simple structure, yet is capable of the most complex conditional logic. For example:

If UserEntry > 10 Then
  'Do Something
ElseIf UserEntry = 17 Then
  'Do something else.
Else
  'Do something entirely different.
End If

NOTE: In Visual Basic, only one condition will run in an if statement. By this, I mean that even if UserEntry is 17 in the example, the second condition will never run because 17 is greater than 10. It is vital to keep this in mind when using Visual Basic.

When using an If statement, it is not required to have any ElseIf or Else statements. In Visual Basic, the only requirements are the "If," the condition, the "Then," and the "End If."

Next we will learn about another type of Conditional logic: The "Select Case" Statement.

Previous Next

Startup Discount
Small Business?
Contact us for an Enormous Discount!