Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology."— Presentation transcript:

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

2 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

3 Arithmetic operations
Operator Operation + Addition - Subtraction * Multiplication / Division \ Integer division Mod Modulus – remainder of division ^ Exponentiation

4 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 .

5 Order of operations The order of operations in arithmetic expressions in programming is as follows: Any operation inside brackets Exponentiation Multiplication and division Integer division Modulus 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).

6 Nested parentheses can be used i. e
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).

7 Activity Indicate what the order of operations will be for the following equations: FirstInt + SecondInt ^ 2 8 / SecondInt / FirstInt FirstInt * (FirstInt + 1) FirstInt * FirstInt + 1 ((SecondInt / FirstInt) + ThirdInt) * 4

8 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.

9 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 - 1 Shortcut version: CountdownInt -=1

10 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

11 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)

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

13 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.

14 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.

15 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.

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


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

Similar presentations


Ads by Google