arithmetic operations and operators, converting data types and formatting programs for output. year...

16
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Upload: kevin-fowler

Post on 30-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Arithmetic operations and operators, converting data types

and formatting programs for output.

Year 11 Information Technology

Page 2: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Main points of chapter 3...

• Variables – can change based on user input• Constant – values that remain the same set in

the program• Calculation – mathematical calculation that

occurs as part of the program• Parse – method of converting data to another

data type

Page 3: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Arithmetic operationsOperator Operation

+ Addition

- Subtraction

* Multiplication

/ Division

\ Integer division

Mod Modulus – remainder of division

^ Exponentiation

Page 4: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

• Integer division (\) – divides one integer by another, giving an integer result and dropping the remainder. E.g. TotalMinutesInt = 150, then HoursInt = TotalMinutesInt \ 60 returns 2 hours for HoursInt.

• Mod – returns the remainder of a division operation. For the above example, MinutesInt = TotalMinutesInt Mod 60 would return 30.

• Exponentiation (^) – raises a number to the power specified .

Page 5: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Order of operations• The order of operations in arithmetic expressions in

programming is as follows:1. Any operation inside brackets2. Exponentiation3. Multiplication and division4. Integer division5. Modulus6. Addition and subtraction

• Nested parentheses can be used i.e. ((Score1Int + Score2Int + Score3Int) + 6) / 3• You can also use nested parentheses for clarity (helps

show what the order of operations will be).

Page 6: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

• Nested parentheses can be used i.e. ((Score1Int + Score2Int + Score3Int) + 6) / 3

• You can also use nested parentheses for clarity (helps show what the order of operations will be).

Page 7: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Activity

• Indicate what the order of operations will be for the following equations:1. FirstInt + SecondInt ^ 22. 8 / SecondInt / FirstInt3. FirstInt * (FirstInt + 1)4. FirstInt * FirstInt + 15. ((SecondInt / FirstInt) + ThirdInt) * 4

Page 8: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Using calculations in code

• These are performed in the assignment statements. Whatever appears on the right side of the assignment operator (=) is assigned to the item on the left. E.g. AmountDueLbl = PriceDec * QuantityDec.ToString() means that the product of the price and quantity will be returned to the Amount Due label.

Page 9: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Assignment operators

• Multiple operators that perform a calculation and assign the result as one operation. The combined operators are: +=, -=, *=, /=, \= and &=.– E.g. Long version: ‘Subtract 1 from variable.

CountdownInt = CountdownInt - 1Shortcut version: CountdownInt -=1

Page 10: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Converting between numeric data types

• Implicit conversions – If converting from one data type to another, where there is no danger of losing precision, the conversion can be performed by an implicit conversion.

From To

Byte Short, Integer, Long, Single, Double, or Decimal

Short Integer, Long, Single, Double, or Decimal

Integer Long, Single, Double, or Decimal

Long Single, Double, or Decimal

Decimal Single, Double

Single Double

Page 11: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

• Explicit conversions – Used to convert data types that do not have implicit conversions. We use convert classes such as ToDecimal, ToSingle etc.– E.g. NumberDec = Convert.ToDecimal

(NumberSingle)

Page 12: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Rounding numbers

• You can use the Decimal.Round method to round decimals. – C2 (currency to decimal places)– P0 (percentage to 0 decimal places)

Page 13: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Formatting data for display

• When you want to display the numeric data in the Text property of a label or text box, you must first convert the value to a string. Using the ToString method and formatting codes, you can choose to display a $, % and so on.

• Format specified codes format the display of the output. See next slide for the format specifier codes.

• Once a value has been formatted, the formatted value can no longer be used in future calculations (i.e. Once a value has been converted to currency in a text box, the text box value cannot be reused.

Page 14: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Format specifier code Name Description

C or c Currency Formats with a dollar sign, commas and 2 decimal places. Negative values are enclosed in parentheses

F or f Fixed-point Formats as a string of numeric digits, no commas, 2 decimal places and a minus sign for negative values.

N or n Number Formats with commas, 2 decimal places and a minus sign for negative values.

D or d Digits Use only for integer data types. Formats with a minus sign for negative values.

P or p Percentage Multiplies the value by 100, adds a space and percentage sign.

Page 15: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Choosing the controls for program output

• You can use either text boxes or labels to display output data, but either should be clearly differentiated from (editable) input areas. In the Windows environment, input areas usually have a white background whilst output should have gray.

• If using a text box rather than a label to display output data, set the ReadOnly property to True (which will immediately change the background colour) and set TabStop property to False.

Page 16: Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology

Programming prac.

• We will work through the programming example on page 133 of ‘Programming in Visual Basic 2008’.