20
IF Statement VB09

IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Embed Size (px)

Citation preview

Page 1: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

IF Statement

VB09

Page 2: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

If statements deal with uncertainty

• If statements allow the computer program to take a different course of action, depending on certain conditions

Page 3: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Syntax of IF statement

If booleanExpression Thenone or more statements

Elseone or more statements

End If

Page 4: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Semantics of IF statement

If booleanExpression Thenone or more statements

Elseone or more statements

End If

If the Boolean expression evaluates to TRUE then execute the first block of statements. Otherwise execute the second block.

Page 5: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Example 1

If raining then

get a taxiElse

walkEnd If

Page 6: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Example 2

If curOwe > curHave Then

blnDebt = TRUE

Else

blnDebt = FALSE

End If

Page 7: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

If statements can be nested

If curTaxOwed > curTadPaid ThenIf blnPolitician Then

blnJail = FALSEElse

blnJail = TRUEEnd If

ElseblnJail = FALSE

End If

Page 8: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

If statements can be nested

If curTaxOwed > curTadPaid ThenIf blnPolitician Then

blnJail = FALSEElse

blnJail = TRUEEnd If

ElseblnJail = FALSE

End If

Page 9: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Add 10% for shipping. Free Shipping for all orders over $20!

If curPrice < 20 then

curShipping = curPrice * 10 / 100

Else

curShipping = 0

End if

curTotal = curPrice + curShipping

Page 10: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Example: Books on the web

• Write a program that accepts as input the price of a book in local currency at four Amazon stores and outputs the name of the cheapest store

Page 11: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Books on the web - Plan

• Input the 4 prices• Convert to Euro where required• Figure out the cheapest• Output the name

Page 12: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Books on the web - Form

Amazon.com

Amazon.co.uk

Amazon.fr

Buy the book at

Calculate

Exit

Page 13: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Books on the web - Form

Amazon.com

Amazon.co.uk

Amazon.fr

Buy the book at

Calculate

Exit

txtUS

txtUK

txtFR

txtCheapest

Attach code to Calculate button

Page 14: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Input Prices

curUSlocal = txtUS.text

curUKlocal = txtUK.text

curFR = txtFR.text

This takes the values from the text boxes and puts them into variables

Page 15: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Convert prices to Euro

curUS = curUSlocal * .99

curUK = curUKlocal * 1.6

This code converts the US $ and UK £ to €. The French price is already in €

Page 16: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Find the cheapestIf (curUS <= curUK) And (curUS <= curFR) Then

-- USA is cheapeststrCheapest = “Amazon.com”

Else-- Contest between Uk and FRIf curUK <= curFrance Then

-- UK is cheapeststrCheapest =

“Amazon.co.uk”Else

-- FR is cheapeststrCheapest =“Amazon.fr”

End ifEnd if

Page 17: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Output Results

txtCheapest.text = strCheapest

Page 18: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Exercise 09.1

• Write a program that accepts the price of a book in local currency at four stores and outputs the location of the cheapest

• Remember that prices at Amazon.de are in €

Page 19: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Exercise 09.2

• Modify the program written for Exercise 09.1 so that the exchange rates are input by the user

Page 20: IF Statement VB09. If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain

Exercise 09.3

• The cost of shipping one book to Ireland is different depending on where it comes from

• Modify the program written for Exercise 09.2 so that the cost of shipping the book is taken into account

$7.98

£3.94

€7.52

€5.60