Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual Basic Statements

Similar presentations


Presentation on theme: "Visual Basic Statements"— Presentation transcript:

1 Visual Basic Statements

2 Relational Operators Equal  = Less than  < Greater than  >
Not equal  <> Less than or equal  <= Greater than or equal  >=

3 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

4 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

5 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

6 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

7 Not A Not A X=3 Y=5 W = X > Y Z = Not X>Y W = False Z = True

8 And A B A And B False True (X > 10) And (X < 20)
is True only when 10 < X <20

9 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

10 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

11 Equivalent A B A Eqv B False True

12 Implies A B A Imp B False True

13 If…Then…End If If expression Then End If Statement(s) True Expression
False

14 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

15 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 ^ * A * C

16 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

17 Solution of Quadratic Equation
Text4.Text = Ans1 Text5.Text = Ans2 End Sub Assuming that 5 text boxes are created. Text1Text3 are used to input A, B & C Text4 & Text5 are used to display answers

18 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

19 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

20 Use of If…Then…Else If…
D = B ^ * 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

21 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

22 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

23 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

24 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

25 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

26 Format Examples Y = Format(1450.0365, “Standard”) Y = “1,450.04”
Y = Format( , “Currency”) Y = “$1,450.04” Y = Format( , “Percent”) Y = “3.66%” Y = Format( , “Scientific”) Y = “1.45E+03”

27 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”

28 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

29 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

30 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

31 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

32 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

33 File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12
Contents of CarData.txt Contents of DataOut.txt

34 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

35 File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12
Contents of CarData.txt Contents of DataOut.txt

36 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

37 File Handling "Suzuki", 3, 4 "Toyota", 5, 7 "Honda", 4, 12
Contents of CarData.txt Contents of DataOut.txt


Download ppt "Visual Basic Statements"

Similar presentations


Ads by Google