Saturday 12 October 2013

Decision Making statements in Visual Basic

                      Decision Making statements in Visual Basic


              Decision making statements are often used to validate certain condition and based on that execute some piece of code. As the name suggest, this statements are used  to check certain conditions. We can have multiple conditions in the statement and according to output, whether the check returns True or False, make some decision in your program.

             There are two types of decision making statements in visual basic, as follows:

  1. If Statements
  2. Select Case Statements



If statements can further be classified into three types:
  • If - Then Statements:  An If - Then statements has only one condition and if that condition gets satisfied that a particular piece of code gets executed
Example:

                         If  name = "XYZ" Then
                                       MsgBox "Welcome" & name
                         End if
  • If -then-else statements: This statements handles both the scenario, what should happen when a condition gets satisfied and what should happens if it doesn't.
Example:

                         If  name = "XYZ" Then
                                       MsgBox "Welcome" & name
                             Else
                                    MsgBox "You are not an authorized person"
                         End if
  • Nested IF-Then-Else Statements: This statements has two or more statements based on which our program acts.
Example:

                         If  name = "XYZ" Then
                                       MsgBox "Welcome" & name
                             Elseif name = "ABC"
                                      MsgBox "Welcome" & name
                             Else
                                      MsgBox "You are not an authorized person"
                         End if

Select Case statements are generally used when there are more conditions to check , which gets quite complicated to handle

Example:

                         Select Case name
Case "XYZ"
                                                MsgBox "Welcome" & name
Case "ABC"
                                                MsgBox "Welcome" & name
Case "IJK"
                                                MsgBox "Welcome" & name
Case "ELSE"
                                                MsgBox "You are not an authorized person"
END SELECT

No comments:

Post a Comment