Presentation is loading. Please wait.

Presentation is loading. Please wait.

The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500.

Similar presentations


Presentation on theme: "The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500."— Presentation transcript:

1 The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500 calories burned. Write a program that allows the user to input the numbers of hours spent at each activity and then calculate the number of pounds worked off.

2

3

4 Operators and Operands in Expressions An Operand can be thought of as something that has a value An Operator is the symbol of an operation carried out on at least 2 operands There are 3 main types of Operator in VB, which take the following Order of Precedence: - Arithmetic Operators - Relational (Comparison) Operators - Logical Operators

5 Expressions An Expression is a Visual Basic Action There are 2 types of Expression in VB: - Simple - Compound An expression is formed by combining Operators and Operands in a meaningful way The purpose of an expression is to perform a calculation and produce a result, or to cause some action to occur

6 Simple Expression: (A+B) Compound Expression: An expression with more than 1 OPERATION is called a Compound Expression [(A+B)*(C-D)] (A, B, C, D) are the Operands (*, +, -) are the Operators Each of these Operator Symbols represents an Operation

7 Arithmetic Operators The hierarchy of operations, or order of precedence, in arithmetic expressions from the highest to the lowest is: [1] Exponentiation: ^ (2 ^ 3 = 8) [2] Multiplication and Division: * / [3] Addition and Subtraction: Example * 2 = 11 However, the order of evaluation can be changed by using parentheses: () (3 + 4) * 2 = 14

8 If one set of parentheses are used inside another set, the parentheses are referred to as nested ((3 + 3) / 3) * 4 = (((4 + 3 * 4) / 2) * 4 + ( / 2 - 3)) = ? Nested

9 What Data Types would be assigned to the Variable X as a result of each of the following Operations (2 + 4) (integer + integer) = integer/Long (2 - 4) (integer - integer) = integer/Long (2 * 4) (integer * integer) = integer/Long (2 / 4) (integer / integer) = Single/Double (Two + Four) (string + string) = string (TwoFour)

10

11

12

13

14 Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be enclosed in parentheses Val an abbreviation for value Val(ExpressionToConvert) The expression that is converted, can be the Property of a Control, a Variable, or a Constant

15 The Val Function returns (produces) a value that can be used as a part of a statement, such as an assignment statement iQuantity = Val(txtQuantity.Text) The Val Function converts an argument to numeric, by beginning at the Left-Most Character If that character is a numeric digit, decimal point, or sign, Val converts the character to numeric and moves to the next character. As soon as a non-numeric character is found, the operation stops

16 Conversion Function The Conversion Function checks if the value stored in the Property of a Control or a Variable is of a specific Data Type required in the execution of a statement(s), and if not the Conversion Function will return a ‘Run-Time Error’ Message The Text in Text Boxes is treated as a String, however in performing Arithmetic Operations (such as ^ * / + -), numeric values are required in the Text property of the Text Boxes

17 VB Conversion Functions CInt - Converts a value to an Integer CLng - Converts a value to a Long Integer CSng - Converts a value to a Single Precision number CDbl - Converts a value to a Double Precision nember CCur - Converts a value to Currency CStr - Converts a value to a String CVar - Converts a value to a Variant

18 CInt Function Dim X As Integer X = CInt(Text1
CInt Function Dim X As Integer X = CInt(Text1.Text) Print X Dim X As Integer, Y As Integer Y = CInt(X) * 2 Print Y CInt(“12”) the brackets are by default - 12 CInt(“Twelve”) - Error CInt(Text1.Text) - the value of the Text in Text1, if it is valid

19

20

21 cHours = 12.2 cPayRate = 5 cTotalPay = ?

22 In the Sample code the Val Function is replaced by the CCur Function
The value stored in the Text Property of the Hours text box is converted from a String to Currency and the value is assigned to the Variable cHours as a Currency Data Type If the value in the Text Property is not consistent with the Currency Data Type, then a ‘Run-Time Error’ will occur and the program will stop executing

23 However, if the value of Hours.Text = 12.2’
PayRate.Text = 5 Using the Val Function and the CCur Function, after multiplication, returns the value of 61 However, if the value of Hours.Text = 12.2’ How would the Functions handle it?

24 The Val Function will convert the value stored in the Property of the control, to a numeric value, before assigning it as the value of the variable for use in an Arithmetic Calculation The Conversion Function checks if the value stored in the Property of a Control is of a specific Data Type required in the execution of a statement(s) (numeric for the purpose of calculation), and if not the Conversion Function will return a ‘Run-Time Error’ Message


Download ppt "The number of calories burned per hour by cycling, jogging and swimming are 200, 475 and 275 respectively. A person loses 1pound of weight for each 3500."

Similar presentations


Ads by Google