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: 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: 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: 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: If condition Then One or more Visual Basic statements Else One or more Visual Basic statements End If Example: Code: 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: 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: ' 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: 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.
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
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
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