Download presentation
Presentation is loading. Please wait.
Published byLinda Hutchinson Modified over 9 years ago
1
IF Statement VB09
2
If statements deal with uncertainty If statements allow the computer program to take a different course of action, depending on certain conditions
3
Syntax of IF statement If booleanExpression Then one or more statements Else one or more statements End If
4
Semantics of IF statement If booleanExpression Then one or more statements Else one or more statements End If If the Boolean expression evaluates to TRUE then execute the first block of statements. Otherwise execute the second block.
5
Example 1 If raining then get a taxi Else walk End If
6
Example 2 If curOwe > curHave Then blnDebt = TRUE Else blnDebt = FALSE End If
7
If statements can be nested If curTaxOwed > curTadPaid Then If blnPolitician Then blnJail = FALSE Else blnJail = TRUE End If Else blnJail = FALSE End If
8
If statements can be nested If curTaxOwed > curTadPaid Then If blnPolitician Then blnJail = FALSE Else blnJail = TRUE End If Else blnJail = FALSE End If
9
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
10
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
11
Books on the web - Plan Input the 4 prices Convert to Euro where required Figure out the cheapest Output the name
12
Books on the web - Form Amazon.com Amazon.co.uk Amazon.fr Buy the book at Calculate Exit
13
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
14
Input Prices curUSlocal = txtUS.text curUKlocal = txtUK.text curFR = txtFR.text This takes the values from the text boxes and puts them into variables
15
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 €
16
Find the cheapest If (curUS <= curUK) And (curUS <= curFR) Then -- USA is cheapest strCheapest = “Amazon.com” Else -- Contest between Uk and FR If curUK <= curFrance Then -- UK is cheapest strCheapest = “Amazon.co.uk” Else -- FR is cheapest strCheapest =“Amazon.fr” End if
17
Output Results txtCheapest.text = strCheapest
18
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 €
19
Exercise 09.2 Modify the program written for Exercise 09.1 so that the exchange rates are input by the user
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.