Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal  <>  Less than.

Presentation on theme: "Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal  <>  Less than."— Presentation transcript:

Visual Basic Statements Chapter 5

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

Logical Expressions ExpressionValueCondition A < BTrueA is less than B FalseA = B or A > B A > BTrueA is greater than B FalseA = B or A < B A <> BTrueA ≠ B FalseA = B Let A = x^2 - 50 Let B = x*(x-70)

Logical Expressions  String comparisons 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 = False

Logical Expressions  String comparisons Less than  Comes before Greater than  Comes after  Uppercase precedes lower case  “ANIMAL” precedes “aNIMAL” (ANIMAL < aNIMAL)  Blank spaces precede nonblank characters  “An imal” precedes “Animal” (An imal < Animal)  String formed by appending characters to another string follows the original string  “Car” precedes “Cart” (Car < Cart)

Logical Operators  Not This  Not  This And That  And  This, That Or Both  Or  Either This or That  Xor

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

And ABA And B False TrueFalse TrueFalse True (X > 10) And (X < 20) is True only when 10 < X <20

Or ABA Or B False True FalseTrue (X > 10) Or (X < 20) (X 20) will always be True will be False when 10 ≤ X ≤ 20

Xor (Exclusive Or) ABA Xor B False True FalseTrue False (X > 10) Xor (X < 20) (X 20) will be False when 10 < X < 20 will be False when 10 ≤ X ≤ 20

If…Then…End If If expression Then Statement(s) End If

Quadratic Equation Roots Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String A = Val(Text1.Text)... D = B ^ 2 - 4 * A * C If D < 0 Then Message = "Roots are complex" End If If D >= 0 Then Message = "Roots are real" End If Picture1.Print Message End Sub

If…Then…Else…End If If expression Then Statement(s) I Else Statement(s) II End If

Quadratic Equation Roots Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String A = Val(Text1.Text)... D = B ^ 2 - 4 * A * C If D < 0 Then Message = "Roots are complex" Else Message = "Roots are real" End If Picture1.Print Message End Sub

If…Then…Else If…Else…End If If expressionA Then Statement set A Else If expressionB Then 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 False Statement Set N True False If Then Else If Then Else If Then Else

Use of If…Then…Else If… Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String... 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 Picture1.Print Message End Sub

If…Then…  Read username from Text1 and write (Username) in (case) letters is (USERNAME) in Text3 where case is read from Text2 and is either Capital or Small.

If…Then… UserName=Text1.Text Opt = Text2.Text If Opt = “Capital” Then OutStr=UserName+” in “+Opt+” letters is “+Ucase(Username) Else OutStr=UserName+” in “+Opt+” letters is “+Lcase(Username) End If Text3.Text=OutStr  What if the user typed capital in Text2?  What if the user entered Urdu in Text2?

If…Then… Private Sub Command1_Click() Dim Username As String, Opt As String, OutStr As String Username = Text1.Text Opt = Text2.Text If UCase(Opt) = "CAPITAL" Then OutStr = Username + " in " + Opt + " letters is " + UCase(Username) ElseIf UCase(Opt) = "SMALL" Then OutStr = Username + " in " + Opt + " letters is " + LCase(Username) Else MsgBox "Please enter “”Capital”” or “”Small”” ",, "ERROR" End If Text3.Text = OutStr End Sub

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 To Numbers; 6 To 8 String; “A” To “Z”, “0” To “9” Logical expression using Is Numbers: Is >= 9 String: Is < “h” Not necessary

Select Case Private Sub Command1_Click() Username = Text1.Text Opt = Text2.Text Select Case LCase(Opt) Case “capital" OutStr = Username + " in " + Opt + " letters is " + UCase(Username) Case “small" OutStr = Username + " in " + Opt + " letters is " + LCase(Username) Case Else MsgBox "Please enter ""Capital"" or ""Small""",, "ERROR" End Select Text3.Text = OutStr End Sub

Using Select Case Private Sub Command1_Click() Dim A as Single, B As Single, C As Single Dim D As Single, Message As String D = B ^ 2 - 4 * A * C Select Case Sgn(D) Case -1 Message = "Complex roots" Case 0 Message = "Real repeated roots" Case 1 Message = "Real distinct roots" End Select Picture1.Print Message End Sub

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 from Alphabet" Case "0" To "9" Message = "Character is Numeric" Case Else Message = "Character isn’t Alphanumeric" End Select

Cost of a phone call  Read CallDuration in seconds  Billing in minutes  Each minute costs Rs 5 up to 10 minutes  Longer than 10 minute call has a flat rate of Rs. 200 per call  121-179 second call costs Rs. 15  601 second call costs Rs. 200

Phone call cost Private Sub Command1_Click() Dim Duration As Integer, Minutes As Single, cost As Integer Duration = Val(Text1.Text) Minutes = Duration / 60 If Minutes - Int(Minutes) > 0 Then Minutes = Int(Minutes) + 1 End If If Minutes > 10 Then cost = 200 Else cost = 5 * Minutes End If Text2.Text = Str(cost) End Sub

Phone call cost Private Sub Command1_Click() Dim Duration As Integer, Minutes As Single, cost As Integer Duration = Val(Text1.Text) Minutes = Duration / 60 If Minutes - Int(Minutes) > 0 Then Minutes = Int(Minutes) + 1 End If Select Case Minutes Case Is > 10 cost = 200 Case Else cost = 5 * Minutes End Select Text2.Text = Str(cost) End Sub Minutes = -Int(-Duration / 60)

Roman Numbers  Write a program that takes decimal numbers (1 – 9) as input, and writes their Roman equivalent. 1  I 2  II 4  IV Extend this program to numbers 1 – 1000 Writing 1000 case or elseif are not good ideas.

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 Y =Abs( Sqr(Abs(D)) / (2 * A)) X = -B / (2 * A) Ans1 = Str(X) & " + i " & Str(Y) Ans2 = Str(X) & " - i " & Str(Y) End If

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

Problem  Convert a decimal number into a binary number. Assume that the decimal number is from 0 to 255. Always represent a number in 8 bits We need to find remainders 8 times Repeat the following set of operations 8 times Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(R) + BinaryNo

Decimal to Binary Conversion Private Sub Command1_Click() Dim DecimalNo As Integer Dim Remainder As Integer Dim BinaryNo As String DecimalNo = Val(Text1.Text) Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Picture1.Print BinaryNo End Sub

Decimal to Binary : Loop Private Sub Command1_Click() Dim DecimalNo As Integer, Remainder As Integer Dim BinaryNo As String DecimalNo = Val(Text1.Text) For Bit = 1 To 8 Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNo Next Bit Picture1.Print BinaryNo End Sub

Loops  In several problems we need to execute same or similar set of statements many times. Re-type the statements as many times  Use loops

For…Next : Syntax For index = value1 To value2 Executable statement(s) Next index For index = value1 To value2 Step value3 Executable statement(s) Next index  value3 = 1 when Step is missing

For..Next; Flow chart value2 > value1; value3>0 value2 < value1; value3<0

For…Next: Rules & Cases  Altering the value of the index is not good programming practice.  Control can be transferred-out, but not in  If (value1=value2) AND (value3<>0)  The loop is executed exactly once  The loop will not be executed if  (value1>value2) And (value3 >=0)  (value1 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/11/3279031/slides/slide_39.jpg", "name": "For…Next: Rules & Cases  Altering the value of the index is not good programming practice.", "description": " Control can be transferred-out, but not in  If (value1=value2) AND (value3<>0)  The loop is executed exactly once  The loop will not be executed if  (value1>value2) And (value3 >=0)  (value1

For…Next : Loop execution Value3Loop Executes if Positive or 0Index <= value2 NegativeIndex >= value2

For…Next : Examples For k = 1 To 10 Picture1.Print k Next k 1 2... 10 For k = 5 To 10 Picture1.Print k Next k 5 6... 10

For…Next : Examples For k = -10 To 0 Picture1.Print k Next k -10 -9... 0 For k = 5 To 1 Step -1 Picture1.Print k Next k 5 4 3 2 1

For…Next : Index example Private Sub Command1_Click() Dim value1 As Integer, Value2 As Integer, Value3 As Integer Dim Index As Integer value1 = Val(Text1.Text) value2 = Val(Text2.Text) value3 = Val(Text3.Text) Picture1.Cls For Index = value1 To value2 Step value3 Picture1.Print Str(Index) + ", "; Next Index Picture1.Print "Index on loop exit " + Str(Index) End Sub

For…Next : Word Reversal Private Sub Command1_Click() Dim Word As String, Letter As String Dim Index As Integer Picture1.Cls Word = Text1.Text For Index = Len(Word) To 1 Step -1 Letter = Mid(Word, Index, 1) Picture1.Print Letter; Next Index End Sub Private Sub Command2_Click() Dim Word As String, Letter As String Dim Index As Integer Picture1.Cls Word = Text1.Text For Index = Len(Word) To 1 Step -1 Letter = LCase(Mid(Word, Index, 1)) If Index = Len(Word) Then Letter = UCase(Letter) End If Picture1.Print Letter; Next Index End Sub

For…Next : Nested Loops  Write a program to display multiplication tables 1  4.

Nested Loops: Tables Private Sub Tables_Click() Dim i As Integer, j As Integer For i = 1 To 10 For j = 1 To 4 Picture1.Print Str(j) + " x " + Str(i) + " = " + Str(j * i) + " "; Next j Picture1.Print Next i End Sub

Download ppt "Visual Basic Statements Chapter 5. Relational Operators  OperationSymbol  Equal  =  Less than  <  Greater than  >  Not equal  <>  Less than."

Similar presentations