Visual Basic Statements
Relational Operators Equal = Less than < Greater than > Not equal <> Less than or equal <= Greater than or equal >=
Logical Expressions Let A = x^2 -50 & B = x *(x – 70) A < B True if A is less than B False if A is not less than B A > B A <> B True if A is not equal to B False if A is equal to B A <= B; A >= B; A = B
Logical Expressions String comparisons Equality FirstName = ”Allama” SecondName = “Muhammad” LastName = “Iqbal” Equality If FirstName=LastName Then… If LastName <> SecondName Then... Inequality (Alphabetic ordering) AFlag = FirstName > Last Name Same as AFlag = True
Logical Expressions String comparisons Less than Comes before Greater than Comes after Uppercase precedes lower case Blank spaces precede nonblank characters Shorter string precedes longer string Car precedes Cart
Logical Operators Not This Not This And That And This, That Or Both Or Either This or That Xor A Equivalent to B Eqv A Implies B Imp
Not A Not A X=3 Y=5 W = X > Y Z = Not X>Y W = False Z = True
And A B A And B False True (X > 10) And (X < 20) is True only when 10 < X <20
Or A B A Or B False True (X > 10) Or (X < 20) will always be True (X < 10) Or (X > 20) will be False when 10 ≤ X ≤ 20
Xor (Exclusive Or) A B A Xor B False True (X > 10) Xor (X < 20) will be False only when 11 ≤ X ≤ 19 (X < 10) Xor (X > 20) will be False when 10 ≤ X ≤ 20
Equivalent A B A Eqv B False True
Implies A B A Imp B False True
If…Then…End If If expression Then End If Statement(s) True Expression False
If…Then…Else…End If Else If expression Then End If Statement(s) I Statement(s) II End If Expression Statement(s) I True False Statement(s) II
Solution of Quadratic Equation Private Sub Command1_Click() Dim A As Integer, B As Integer, C As Integer Dim D As Single, X As Single, Y As Single Dim Ans1 As String, Ans2 As String Dim X1 As Single, X2 As Single ‘ Read Coefficients of the quadratic equation A = Val(Text1.Text) 'Coefficient of x^2 B = Val(Text2.Text) 'Coefficient of x C = Val(Text3.Text) 'Constant ‘ Evaluate Discriminant as D D = B ^ 2 - 4 * A * C
Solution of Quadratic Equation If D >= 0 Then ' Roots are real. Equal or unequal X1 = ( -B + Sqr(D) ) / (2 * A) X2 = ( -B - Sqr(D) ) / (2 * A) Ans1 = Str(X1) Ans2 = Str(X2) Else ' Roots are complex. Just need the absolute value of the imag. part X =Abs( Sqr(Abs(D)) / (2 * A)) Y = -B / (2 * A) Ans1 = Str(Y) & " + i " & Str(X) Ans2 = Str(Y) & " - i " & Str(X) End If
Solution of Quadratic Equation Text4.Text = Ans1 Text5.Text = Ans2 End Sub Assuming that 5 text boxes are created. Text1Text3 are used to input A, B & C Text4 & Text5 are used to display answers
If…Then…Else If…Else…End If If expressionA Then Statement set A Else If expressionB Statement Set B Else If expressionC Then . Else Statement Set N End If
If…Then…Else If…Else…End If False False Expression A Expression B Expression k False True True True Statement Set A Statement Set B Statement Set k Statement Set N
Use of If…Then…Else If… D = B ^ 2 - 4 * A * C If D < 0 Then Message = "Roots are complex" ElseIf D = 0 Then Message = "Roots are real & repeated" ElseIf D > 0 Then Message = "Roots are real & distinct" End If
Select Case . Select Case expression Case value set 1 Statement Set 1 Case Else Statement Set N End Select List separated by commas Values; 1, 3, 5 String(s); “a”, “5” Range using To Numbers; 6 To 8 String; “A” To “Z” , “0” To “9” Logical expression using Is Numbers: Is >= 9 String: Is < “h” Not necessary
Using Select Case D = B ^ 2 - 4 * A * C n = Sgn(D) Select Case n Message = "Complex roots" Case 0 Message = "Real repeated roots" Case 1 Message = "Real distinct roots" End Select
Using Select Case D = B ^ 2 - 4 * A * C Select Case D Case Is < 0 Message = "Complex roots" Case Is = 0 Message = "Real repeated roots" Case Is > 0 Message = "Real distinct roots" End Select
Using Select Case Dim Character As String Select Case Character Case "A" To "Z", "a" To "z" Message = "Character is Alphabet" Case "0" To "9" Message = "Character is Numeric" Case Else Message = "Character is not Alphanumeric" End Select
Format Library function Y = Format(x, “formatstring”) Returns the value of x in a format designated by “formatstring” Some built-in format functions Standard Currency Percent Scientific Long Date Medium Date
Format Examples Y = Format(1450.0365, “Standard”) Y = “1,450.04” Y = Format(1450.0365, “Currency”) Y = “$1,450.04” Y = Format(0.03657, “Percent”) Y = “3.66%” Y = Format(1450.0365, “Scientific”) Y = “1.45E+03”
Format Examples Y = Format(Now, “Long Date”) Y = “Wednesday, October 1, 2003” Y = Format(Now, “Medium Date”) Y = “01-Oct-03” Y = Format(Now, “hh:mm:ss am/pm”) Y = “09:07:23 am”
Format Examples Y = Format(“CS101”, “@@@@@@@@”) Y = “bbbCS101” b stands for blank. Or CS101 is right-justified. Y = Format(“CS101”, “&&&&&&&&”) Y = “CS101” Right blanks are truncated b stands for blank. Or CS101 is left-justified. CS101
File Handling Files are used for large data input/output Create a .txt file called CarData by using Notepad (or ASCII file using other software) with following contents “Suzuki”, 3, 4 “Toyota”, 5, 7 “Honda”, 4, 15
File Handling Files must be Opened before any read/write operation. Files must be Closed after any read/write operation Syntax Open “CarData” For Input As #138 Opens a file called CarData.txt for read-only and this file will be called #138 The file reference number can be from 1 to 255
File Handling Close #138 Input #138, Make, NoOfModels, NoOfColors Assumes that the 1st line of “CarData.txt” contains 3 values corresponding to the three variables. Make should be a string variable, while NoOfModels and NoOfColors is a numeric type (Integer etc) After this Input statement Make = “Suzuki” NoOfModels = 3 NoOfColors = 4 Close #138
File Handling Dim Make As String, NoOfModels As Integer Dim NoOfColors As Integer Open "C:\CarData.txt" For Input As #1 Input #1, Make, NoOfModels, NoOfColors Open "C:\DataOut.txt" For Output As #2 Write #2, Make, NoOfColors, NoOfModels Close #1 Close #2
File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12 Contents of CarData.txt Contents of DataOut.txt
File Handling Dim Make As String, NoOfModels As Integer Dim NoOfColors As Integer, FMake As String Open "c:\CarData.txt" For Input As #1 Open "c:\DataOut.txt" For Output As #2 Input #1, Make, NoOfModels, NoOfColors FMake = Format(Make, "@@@@@@@@") Write #2, FMake, NoOfColors, NoOfModels Close #1 Close #2
File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12 Contents of CarData.txt Contents of DataOut.txt
File Handling Dim Make As String, NoOfModels As Integer Dim NoOfColors As Integer, FMake As String Open "c:\CarData.txt" For Input As #1 Open "c:\DataOut.txt" For Output As #2 Input #1, Make, NoOfModels, NoOfColors FMake = Format(Make, "@@@@@@@@") Write #2, FMake, NoOfColors, NoOfModels FMake = Format(Make, "&&&&&&&&") Close #1 Close #2
File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12 Contents of CarData.txt Contents of DataOut.txt