Program Flow

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

 

Structured programming is based around three basic constructs: 1. Sequence,< Selection and Iteration. The Sequence construct is the simplest, whereby statements are executed in the order they're found in the code.

Both the Selection and Iteration constructs require a Logical Test. Logical tests can be performed on Variables, literal values, calculations, results of functions and controls. The test will result in either True, False, Null or Empty.


SELECTION CONSTRUCTS

The Selection constructs are used when the flow of execution may flow down two or more paths.

THE IF STATEMENT

The If statement is used to control the flow of execution down one of two or more paths, depending on the result of a logical test. The ElseIf statement may be used to specify conditions should previous conditions be false, and Else may be used should none of the above be true.

If grade < 40 Then
     Debug.Print "Failed"
ElseIf grade < 75 Then
     Debug.Print "Passed"
Else
     Debug.Print "Excellent Pass"
End If

If statements can be nested, and the combined with logical operators.

 

 

THE SELECT STATEMENT:

The Select statement can be used when multiple If statements become messy and difficult to read. Each Case statement is evaluated from the top down. Should the Case statement be true, the section of code associated with the condition is executed. Case Else is used as a catch all.
The following example use the Select Case construct to move a Shape, shpBlock, according to which arrow key is pressed on the keyboard.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
     Select Case KeyCode
          Case vbKeyUp
               ' Move the block up the form
               shpBlock.Top = shpBlock.Top - 50
          Case vbKeyDown
               ' Move the block down the form
               shpBlock.Top = shpBlock.Top + 50
          Case vbKeyLeft
               ' Move the block left on the form
               shpBlock.Left = shpBlock.Left - 50
          Case vbKeyRight
               ' Move the block right on the form
               shpBlock.Left = shpBlock.Left + 50
          Case Else
               MsgBox "Incorrect Key", vbExclamation, "KeyDown Demonstration"
     End Select
End Sub

 

It's possible to test for ranges using the Is operator. The following example uses the Is operator to find grades in a range.

Select Case grade
     Case Is < 40
          Debug.Print "Failed"
     Case Is < 75
          Debug.Print "Passed"
     Case Else
          Debug.Print "Excellent Pass"
End Select

 

 

ITERATION CONSTRUCTS:

The Iteration constructs are used when a block of code is required to be executed continually until a condition is met.

PRE-CONDITION LOOPS

Pre-condition loops allow for a condition to be tested at the start of the loop. This type of loop is used when you only want to execute the block of code should the condition be true. For example, you would only want to iterate through a RecordSet collection should the RecordSet contain some records.

Do While Not adoRS.EOF
     Debug.Print adoRS.Fields("Surname").Value
     adoRS.MoveNext
Loop

 

The following example of a pre-condition loop calculate the factorial of a number. (a number that is multiplied by every integer number below itself down to 1). The value is taken from a TextBox, txtFactorial.

Private Sub cmdFactorial_Click()
     Dim factorial As Long
     Dim counter As Integer
     Dim strAnswer As String
     counter = Val(txtFactorial.Text)
     factorial = counter
     Do While counter > 1
          counter = counter - 1
          factorial = factorial * counter
     Loop
     strAnswer = "Factorial of " & txtFactorial.Text & " is " & factorial
     MsgBox strAnswer, vbInformation, "Factorial"
End Sub

 

 

POST-CONDITION LOOPS:

Post-condition loops allow for a condition to be tested at the end of the loop. This type of loop is used when you want to execute the block of code at least once. This is useful for validation.
The following example prompts the user for a number between 1 and 10. If a number is not entered in that range, the program continues to prompt for the number.

Dim response As Integer
Do
     response = Val(InputBox("Enter a number between 1 and 10", "Number Cruncher"))
Loop While response < 1 Or response > 10

 

 

COUNTED LOOPS:

Sometimes the number of iterations required are known in advance. In this case a counted loop may be used. Visual Basic provides a For ... Next construct to handle counted loops.
The following example prints the numbers one to ten.

For counter = 1 To 10
     Debug.Print counter & vbCrLf
Next counter

 

An optional Step size may be added after the end value, should you wish the increment to be anything other than 1. The step size may be positive, negative, integer or fractional. Negative Step values are used to count backwards.
The following example prints the numbers ten to one.

For counter = 10 To 1 Step -1
     Debug.Print counter & vbCrLf
Next counter
 


If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

+91-9967648641

+91-9967648641