Visual Basic: Relational Operators, Loops, and Control Lists

Relational Operators in Visual Basic

Relational operators are used to compare values in Visual Basic. Here’s a breakdown of common operators:

  • = (Equals): The expression a = b evaluates to True if a is equal to b, and False otherwise.
  • > (Greater Than): The expression a > b evaluates to True if a is greater than b, and False otherwise.
  • < (Less Than): The expression a < b evaluates to True if a is less than b, and False otherwise.
  • >= (Greater Than or Equal To): The expression a >= b evaluates to True if a is greater than or equal to b, and False otherwise.
  • <= (Less Than or Equal To): The expression a <= b evaluates to True if a is less than or equal to b, and False otherwise.
  • <> (Not Equal To): The expression a <> b evaluates to True if a is not equal to b, and False otherwise.

Select Case Structure

The Select Case structure is a multiple selection structure. The program evaluates each condition and executes the statements for the case where the condition is True. It can be used as a replacement for complex If structures.

Repetitive Loops

Repetitive loops allow a set of instructions to be executed repeatedly while a given condition is met. These instructions are called the loop body. Visual Basic provides several ways to create loops:

  • For...Next
  • While...Wend
  • Do...Loop

Control Lists

The purpose of a control list is to provide a list of elements from which the user can select one. It can also be used to display a data set as a list.

List Control Methods

  • AddItem: Adds a new item to the list.
  • RemoveItem: Deletes a specific element from the list.
  • Clear: Removes all items and leaves the list empty.

For…Next Loops

The For...Next loop is used to create a loop with a predefined number of iterations. It is suitable when you know the exact number of times the instructions should be repeated.

For Each…Next Loops

The For Each...Next loop is used to iterate through arrays, repeating the same actions with each element of the array.

Loop Control Elements

  • Variable = Initial Value: Initializes the loop counter, setting the starting value of the counter variable.
  • Step: Specifies the increment or decrement of the variable each time the loop ends. If not specified, the variable increases by one unit.
  • To Final Value: Specifies the value at which the loop must stop.
  • Next: Sends the program back to the first line of the loop to perform the relevant checks and continue the loop if the conditions are met.

Do…Loop Statements

The Do...Loop statement repeats the execution of a loop while a certain condition is met (While), or until a condition is met (Until). The condition can be evaluated at the beginning or end of the loop.

Do Loop Variations

  • Do While (Condition)…Loop: The loop is executed while the condition is True, and the condition is evaluated at the beginning.
  • Do Until (Condition)…Loop: The loop is executed until the condition is True, and the condition is evaluated at the beginning.
  • Do…Loop While (Condition): The loop is executed while the condition is True, and the condition is evaluated at the end.
  • Do…Loop Until (Condition): The loop is executed until the condition is True, and the condition is evaluated at the end.

Do While…Loop

In this statement, the loop control is done through a condition that is evaluated at the beginning. While the condition is True, the loop is executed. When the condition is False, the loop stops executing.

Do Until…Loop

In this loop control, the decision is made by a condition that is evaluated at the beginning. The loop will be repeated until the condition is met. When the condition is met, the loop ends.

Do…While Loop

In this structure, the loop control is done via a condition that is evaluated after each iteration of the loop. While the condition is True, the loop will continue to repeat. When the condition is False, the loop stops, and the program continues with the subsequent instructions.

Do…Loop Until

One of its most common applications is the control of the data that are introduced during the execution of the program. The only difference between Do...While and Do...Until is the condition (they are contrary). One statement will repeat the loop while the condition is met, and the other statement will repeat the loop until the condition is met.