Download presentation

Presentation is loading. Please wait.

Published byLisbeth Bennet Modified over 2 years ago

1
Visual Basic Statements Chapter 5

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

3
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)

4
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

5
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)

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

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

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

9
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

10
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

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

12
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

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

14
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

15
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

16
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

17
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

18
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.

19
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?

20
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

21
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

22
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

23
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

24
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

25
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

26
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

27
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

28
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)

29
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.

30
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

31
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

32
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

33
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

34
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

35
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

36
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

37
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

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

39
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

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

41
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

42
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

43
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

44
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

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

46
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

Similar presentations

OK

1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.

1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on pharmaceutical industry in india Ppt on mammals and egg laying animals pictures Ppt on question tags songs Ppt on marie curie high school Ppt on automobile related topics about information Presenting ppt on ipad Free download ppt on flow of control in c++ Ppt on 5 pen pc technology Ppt on two point perspective definition Ppt on leadership theories