Visual Basic Statements. Relational Operators Equal = Less than < Greater than > Not equal Less...
-
date post
21-Dec-2015 -
Category
Documents
-
view
217 -
download
5
Transcript of Visual Basic Statements. Relational Operators Equal = Less than < Greater than > Not equal Less...
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 comparisonsFirstName = ”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
True False
False True
X=3Y=5
W = X > YZ = Not X>Y
W = FalseZ = True
And
A B A And B
False False False
False True False
True False False
True True True
(X > 10) And (X < 20)is True only when
10 < X <20
Or
A B A Or B
False False False
False True True
True False True
True True 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 False False
False True True
True False True
True True False
(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 False True
False True False
True False False
True True True
Implies
A B A Imp B
False False True
False True True
True False False
True True True
If…Then…End If
If expression ThenStatement(s)
End If
Expression
Statement(s)
True
False
If…Then…Else…End If
If expression ThenStatement(s) I
ElseStatement(s) II
End If
Expression
Statement(s) I
TrueFalse
Statement(s) II
Solution of Quadratic Equation
Private Sub Command1_Click()Dim A As Integer, B As Integer, C As IntegerDim D As Single, X As Single, Y As SingleDim Ans1 As String, Ans2 As StringDim X1 As Single, X2 As Single‘ Read Coefficients of the quadratic equationA = Val(Text1.Text) 'Coefficient of x^2B = Val(Text2.Text) 'Coefficient of xC = Val(Text3.Text) 'Constant‘ Evaluate Discriminant as DD = B ^ 2 - 4 * A * C
Solution of Quadratic EquationIf 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 = Ans1Text5.Text = Ans2End 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 IfIf 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
Expression A
Statement Set A
Expression B
Statement Set B
Expression k
Statement Set k
True True
False False
Statement Set N
TrueFalse
Use of If…Then…Else If…
D = B ^ 2 - 4 * A * CIf 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 value set 2
.
.Case Else
Statement Set N
End Select
List separated by commas Values; 1, 3, 5 String(s); “a”, “5”Range using ToNumbers; 6 To 8String; “A” To “Z” , “0” To “9”Logical expression using IsNumbers: Is >= 9String: Is < “h”
Not necessary
Using Select CaseD = B ^ 2 - 4 * A * Cn = Sgn(D)Select Case nCase -1 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 * CSelect Case DCase Is < 0 Message = "Complex roots"Case Is = 0 Message = "Real repeated roots"Case Is > 0 Message = "Real distinct roots"End Select
Using Select CaseDim Character As StringSelect Case CharacterCase "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.
CS101CS101
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
• SyntaxOpen “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
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 IntegerDim NoOfColors As IntegerOpen "C:\CarData.txt" For Input As #1Input #1, Make, NoOfModels, NoOfColorsOpen "C:\DataOut.txt" For Output As #2Write #2, Make, NoOfColors, NoOfModelsInput #1, Make, NoOfModels, NoOfColorsWrite #2, Make, NoOfColors, NoOfModelsInput #1, Make, NoOfModels, NoOfColorsWrite #2, Make, NoOfColors, NoOfModelsClose #1Close #2
File Handling
"Suzuki", 3, 4
"Toyota", 5, 7
"Honda", 4, 12
"Suzuki",4,3
"Toyota",7,5
"Honda",12,4
Contents of CarData.txt
Contents of DataOut.txt
File HandlingDim Make As String, NoOfModels As IntegerDim NoOfColors As Integer, FMake As StringOpen "c:\CarData.txt" For Input As #1Open "c:\DataOut.txt" For Output As #2Input #1, Make, NoOfModels, NoOfColorsFMake = Format(Make, "@@@@@@@@")Write #2, FMake, NoOfColors, NoOfModelsInput #1, Make, NoOfModels, NoOfColorsFMake = Format(Make, "@@@@@@@@")Write #2, FMake, NoOfColors, NoOfModelsInput #1, Make, NoOfModels, NoOfColorsFMake = Format(Make, "@@@@@@@@")Write #2, FMake, NoOfColors, NoOfModelsClose #1Close #2
File Handling
"Suzuki", 3, 4"Toyota", 5, 7"Honda", 4, 12
" Suzuki",4,3" Toyota",7,5" Honda",12,4
Contents of CarData.txt
Contents of DataOut.txt
File HandlingDim Make As String, NoOfModels As IntegerDim NoOfColors As Integer, FMake As StringOpen "c:\CarData.txt" For Input As #1Open "c:\DataOut.txt" For Output As #2Input #1, Make, NoOfModels, NoOfColorsFMake = Format(Make, "@@@@@@@@")Write #2, FMake, NoOfColors, NoOfModelsInput #1, Make, NoOfModels, NoOfColorsFMake = Format(Make, "&&&&&&&&")Write #2, FMake, NoOfColors, NoOfModelsInput #1, Make, NoOfModels, NoOfColorsFMake = Format(Make, "@@@@@@@@")Write #2, FMake, NoOfColors, NoOfModelsClose #1Close #2
File Handling
"Suzuki", 3, 4"Toyota", 5, 7"Honda", 4, 12
" Suzuki",4,3"Toyota",7,5" Honda",12,4
Contents of CarData.txt
Contents of DataOut.txt