Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Decision Making in Visual Basic (http://www.go4expert.com/articles/decision-visual-basic-t3697/)

Sanskruti 31Mar2007 19:59

Decision Making in Visual Basic
 
All comparison operators produce true or false results. In other words, the comparison is either true or the comparison is false. The mathematical operators produce numeric values, whereas the comparison operators produce only true or false values. The rest of the program can use the true or false comparison operator result to make decisions. Comparison operators are operators that compare data values against each other and produce true or false results.

The If Statement



Perhaps the most important statement in a program is the If statement and then its statements. If performs one of two possible code actions, depending on the result of the comparison. In other words, If uses comparison operator results to test data. If might execute one or more lines of subsequent code, depending on the result of a comparison.

Before If, the code you wrote executed sequentially, one statement after another. If lets your program be more decisive and execute only parts of the program if the data warrants partial execution. For example, suppose you were writing an invoicing system. In such a system, no sales tax should be computed for tax-exempt organizations, so your program would skip over the tax computation code when processing such organizations. If makes decisions. If a condition is true, the body of the If statement executes.

Here is one format of If:
Code: VB

If Condition Then
    One or more Visual Basic statements
End If

End If tells Visual Basic where the body of the If statement ends.

Example:
Code: VB

Dim a As Integer
Dim b As Integer
a = 3
b = 4
If a > b Then
    MsgBox ("a is greater then b")
Else
    MsgBox ("b is greater then a")
End If

The parentheses are not required around the condition in an If, but they help separate the test from the rest of the code. In addition, the indentation helps illustrate the code that appears inside the If statement's body.

There is a shortcut form of If that you might run across. The single-line If statement has a format that looks like this:
Code: VB

If condition Then VBStatement

The single-line If doesn't require an End If statement because the condition and the body of the If reside on the same line. Single-line If statements don't provide for easy program maintenance. If you decide that you want to add to the body of the If, you must convert the single-line If to a multiple-line If, and you might forget to then add End If. Therefore, even if the body of an If statement takes only one line, code the If as a multiple-line If...End If statement to make the program more maintainable.

If Statement's Else



Whereas If executes code based on the condition's true condition, the Else statement executes code based on the condition's false condition. Else is an optional part of the If statement. Else specifies the code that executes if the condition is false.

The complete format of the If statement with Else is as follows:

Code: vb

If condition Then
  One or more Visual Basic statements
Else
  One or more Visual Basic statements
End If

Example:

Code: VB

num = Int(Text1.Text) 'Input

If (num > 0) Then
    strReply = "You have a car!"
Else
    strReply = "You don't have a car!"
End If

If (num > 1) Then strReply = "Are you rich!"

'display our comment
Label1.Caption = strReply

Select Case



If you have a lot of conditional statements, using If..Then..Else could be very messy. For multiple conditional statements, it is better to use Select Case the format is :
Code: VB

Select Case expression
   Case value1
        Block of one or more VB statements
   Case value2
        Block of one or more VB Statements
   Case value3
        Block of one or more VB statements
   Case value4
        .
        .
        .
   Case Else
        Block of one or more VB Statements
End Select

The data type specified in expression must match that of Case values.

Example:

Code: VB

' Examination Grades
Dim grade As String
Private Sub Compute_Click( )
    grade=txtgrade.Text
    Select Case grade
      Case  "A"
           result.Caption="High Distinction"
      Case "A-"
          result.Caption="Distinction"
      Case "B"
            result.Caption="Credit"
      Case "C"
            result.Caption="Pass"
      Case Else
            result.Caption="Fail"
      End Select
End Sub

Please note that grade is a string, so all the case values such as "A" are of String data type.

Example:
Code: VB

Dim mark As Single
Private Sub Compute_Click()
    'Examination Marks
     mark = mrk.Text
    Select Case mark
    Case Is >= 85
        comment.Caption = "Excellence"
    Case Is >= 70
        comment.Caption = "Good"
    Case Is >= 60
        comment.Caption = "Above Average"
    Case Is >= 50
        comment.Caption = "Average"
    Case Else
        comment.Caption = "Need to work harder"
    End Select
End Sub

Note we use the keyword Is here to impose the conditions. This is generally used for numeric data.

Steel9561 1May2008 02:55

Re: Decision Making in Visual Basic
 
I think that decisions are used in all programs and this is taught in introduction to programming books. Basically you make decisions based on what you think the user will do in the program. Depending on the user selections you then take a specific action that you want the program to do.

Luis Lazo

faizulhaque 4Jun2008 13:45

Re: Decision Making in Visual Basic
 
Any one tell me who two condition is used in if statement
as in Turbo C++ we use

if (statement && statement)
{
statement
else
statement
}
operator are allowed in VB
like NOT, AND, OR

shabbir 4Jun2008 20:22

Re: Decision Making in Visual Basic
 
Using the And Operator

faizulhaque 5Jun2008 09:15

Re: Decision Making in Visual Basic
 
thanks u Shabbir for your assistance.

I got and operator (gate) ' Or ' from a boxx mastring VB 6.0.

I got my problem solved.
Thanks a lot

shabbir 5Jun2008 10:29

Re: Decision Making in Visual Basic
 
My pleasure.

faizulhaque 6Jun2008 12:14

Re: Decision Making in Visual Basic
 
MSDN for VB?

pl any one tell frm where i got that.


All times are GMT +5.5. The time now is 08:23.