Visual Basic Statements Chapter 5. Relational Operators OperationSymbol Equal = Less than < ...

46
Visual Basic Statements Chapter 5

Transcript of Visual Basic Statements Chapter 5. Relational Operators OperationSymbol Equal = Less than < ...

Visual Basic Statements

Chapter 5

Relational Operators

Operation Symbol Equal = Less than < Greater than > Not equal <> Less than or equal <= Greater than or equal >=

Logical Expressions

Expression Value Condition

A < B True A is less than BFalse A = B or A > B

A > B True A is greater than BFalse A = B or A < B

A <> B True A ≠ BFalse A = B

Let A = x^2 - 50Let B = x*(x-70)

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 = 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

NotA Not A

True False

False True

X=3Y=5

W = X > YZ = Not X>Y

W = FalseZ = True

AndA B A And B

False False False

False True False

True False False

True True True

(X > 10) And (X < 20)

is True only when10 < X <20

OrA B A Or B

False False False

False True True

True False True

True True True

(X > 10) Or (X < 20)

(X < 10) Or (X > 20)

will always be True

will be False when10 ≤ 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)

(X < 10) Xor (X > 20)

will be False when10 < X < 20

will be False when10 ≤ X ≤ 20

If…Then…End If

If expression Then

Statement(s)End If Expression

Statement Set

True

False

If

Then

End If

Quadratic Equation Roots

Private Sub Command1_Click()Dim A as Single, B As Single, C As SingleDim D As Single, Message As StringA = Val(Text1.Text)...D = B ^ 2 - 4 * A * CIf D < 0 Then Message = "Roots are complex"End IfIf D >= 0 Then Message = "Roots are real"End IfPicture1.Print MessageEnd Sub

If…Then…Else…End If

If expression ThenStatement(s) I

ElseStatement(s) II

End If

Expression

Statement Set #1Statement Set #2

TrueFalse

If

ThenElse

End If

Quadratic Equation Roots

Private Sub Command1_Click()Dim A as Single, B As Single, C As SingleDim D As Single, Message As StringA = Val(Text1.Text)...D = B ^ 2 - 4 * A * CIf D < 0 Then Message = "Roots are complex"Else Message = "Roots are real"End IfPicture1.Print MessageEnd Sub

If…Then…Else If…Else…End IfIf expressionA Then

Statement set A

Else If expressionB ThenStatement 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

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 SingleDim D As Single, Message As String...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 IfPicture1.Print MessageEnd 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 NEnd 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 CasePrivate Sub Command1_Click()Dim A as Single, B As Single, C As SingleDim D As Single, Message As StringD = B ^ 2 - 4 * A * CSelect Case Sgn(D)

Case -1 Message = "Complex roots"

Case 0 Message = "Real repeated roots"

Case 1 Message = "Real distinct roots"End SelectPicture1.Print MessageEnd Sub

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 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 IntegerDuration = Val(Text1.Text)Minutes = Duration / 60If Minutes - Int(Minutes) > 0 Then Minutes = Int(Minutes) + 1End IfIf Minutes > 10 Then cost = 200Else cost = 5 * MinutesEnd IfText2.Text = Str(cost)End Sub

Phone call cost

Private Sub Command1_Click()Dim Duration As Integer, Minutes As Single, cost As IntegerDuration = Val(Text1.Text)Minutes = Duration / 60If Minutes - Int(Minutes) > 0 Then Minutes = Int(Minutes) + 1End IfSelect Case Minutes Case Is > 10 cost = 200 Case Else cost = 5 * MinutesEnd SelectText2.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 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 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 = 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

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

timesRemainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(R) + BinaryNo

Decimal to Binary Conversion

Private Sub Command1_Click()Dim DecimalNo As IntegerDim Remainder As IntegerDim BinaryNo As StringDecimalNo = Val(Text1.Text)Remainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNoRemainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNoRemainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNo

Remainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNoRemainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNoRemainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNoRemainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNoRemainder = DecimalNo Mod 2DecimalNo = DecimalNo \ 2BinaryNo = Str(Remainder) + BinaryNoPicture1.Print BinaryNoEnd Sub

Decimal to Binary : Loop

Private Sub Command1_Click()Dim DecimalNo As Integer, Remainder As IntegerDim BinaryNo As StringDecimalNo = Val(Text1.Text)For Bit = 1 To 8 Remainder = DecimalNo Mod 2 DecimalNo = DecimalNo \ 2 BinaryNo = Str(Remainder) + BinaryNoNext BitPicture1.Print BinaryNoEnd 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

Index = value1

Index > value2

Statement(s)

Index=Index+value3Yes

No

Index = value1

Index < value2

Statement(s)

Index=Index+value3Yes

No

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<value2) And (value3<0)

For…Next : Loop execution

Value3 Loop Executes if

Positive or 0 Index <= value2

Negative Index >= value2

For…Next : Examples

For k = 1 To 10Picture1.Print k

Next k

12...10

For k = 5 To 10Picture1.Print k

Next k

56...10

For…Next : Examples

For k = -10 To 0Picture1.Print k

Next k

-10-9...0

For k = 5 To 1 Step -1

Picture1.Print kNext k

54321

For…Next : Index example

Private Sub Command1_Click()Dim value1 As Integer, Value2 As Integer, Value3 As IntegerDim Index As Integervalue1 = Val(Text1.Text)value2 = Val(Text2.Text)value3 = Val(Text3.Text)Picture1.ClsFor Index = value1 To value2 Step value3 Picture1.Print Str(Index) + " , ";Next IndexPicture1.Print "Index on loop exit " + Str(Index)End Sub

For…Next : Word Reversal

Private Sub Command1_Click()Dim Word As String, Letter As StringDim Index As IntegerPicture1.ClsWord = Text1.TextFor Index = Len(Word) To 1 Step -1 Letter = Mid(Word, Index, 1) Picture1.Print Letter;Next IndexEnd Sub

Private Sub Command2_Click()Dim Word As String, Letter As StringDim Index As IntegerPicture1.ClsWord = Text1.TextFor 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 IndexEnd 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 IntegerFor i = 1 To 10 For j = 1 To 4 Picture1.Print Str(j) + " x " + Str(i) + " = " + Str(j * i) + " "; Next j Picture1.PrintNext iEnd Sub