Parag’s Den

Dedicated to my adored friends…..

06- Conditionl Logic

Posted by paragdongre on June 4, 2008


Conditional Operators

Operators that describe or state a relationship on one side of an expression with the other side of an expression is called a relational or conditional operator. The “=” sign is an operator that states that the values on the left side of the expression is equal to the right side of the expression, thus making it an equation. There are many other conditional operators that you can use in Visual Basic. They are the following.

  • The “<” sign is used to describe an expression where the left side is “less than” the right side.
  • The “>” sign is used to describe an expression where the left side is “greater than” the right side.
  • The “<=” sign is used to describe an expression where the left side is “less than OR equal to” the right side.
  • The “>=” sign is used to describe an expression where the left side is “greater than OR equal to” the right side.
  • The “<>” sign is used to describe an expression where the left side is “not equal to” the right side.

 

Combining Conditional Operators

You can combine the use of conditional operators using the following commands.

The “AND” command combines two conditional operators together. See the code below for an example.

 If x > 5 and y < 3 then z = x – y

The “OR” command selects which one of the two conditional operators are satisfied or dissatisfied. See the code below for an example.

 If x < 5 or y > 6 then z = x + y

 

If – Then

The If-Then statement is used to execute commands given that a set of conditions are satisfied. The basic structure of an If-Then statement is like the one below.

If x < 4 then
 x = x + 1
End If

OR

If [condition] then 
 [command lines]
End If

You can also put additional commands if a condition does NOT hold. You can use the “Else” command to do this. An example of how this is used is shown below.

If x < 4 then
 z = x + 3
Else
 z = x + 9
End If

OR

If [condition] then
 [command lines]
Else
 [command lines]
End If

 

Select Case

The Select Case statements are similar to the If-Then statements. The Select Case statements are used for executing different command lines in several different circumstances. For example, you can run a specific set of commands in scenario one, a specific set of commands in scenario two, and another set of commands in scenario three. Look at the code below and see how the Select Case logic works.

Sub ()

        Dim x As Integer

        Dim result As String

 

        x = 0

        result = “no value”

 

        Select Case x

            Case Is < 0

                result = “x is a negative integer”

            Case Is = 0

                result = “x is neither a positive nor a negative integer”

            Case Is > 0

                result = “x is a positive integer”

        End Select

 

        MsgBox(result)

              

End Sub

In the code above, you can see that the value for x can have three possible outcomes in the message box. Try changing the values of x to -4 and then to 9.

The basic structure of a Select Case code is as follows

        
        Select Case [variable]
               Case [1]
                       [command lines]
               Case [2]
                       [command lines]
               .
               .
               .
               Case [N]
                       [command lines]
        End Select

 

Conditional Logic and User Form

Make a new user form and arrange it as seen below.

You can toggle the starting user form of your application by selecting “My Project” on the Solution Explorer window and then choose “Startup form.”

Double click the button in the middle of your new user form and then write code that will identify if x is a positive, a negative, or neither a positive nor a negative integer. Hint: Review the code awhile ago and infuse it to the user form.

The code for identifying if x is a positive or a negative integer may look similar to the code below.

 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim x As Integer
        Dim result As String

        x = TextBox1.Text

        Select Case x
            Case Is < 0
                result = “Negative”
            Case Is = 0
                result = “Neither”
            Case Is > 0
                result = “Positive”
        End Select

        TextBox2.Text = result

  End Sub

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: