Download presentation
Presentation is loading. Please wait.
Published byKevin Fowler Modified over 9 years ago
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 OperatorOperation +Addition -Subtraction *Multiplication /Division \Integer division ModModulus – 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: 1.Any operation inside brackets 2.Exponentiation 3.Multiplication and division 4.Integer division 5.Modulus 6.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. ((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: 1.FirstInt + SecondInt ^ 2 2.8 / SecondInt / FirstInt 3.FirstInt * (FirstInt + 1) 4.FirstInt * FirstInt + 1 5.((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. FromTo ByteShort, Integer, Long, Single, Double, or Decimal ShortInteger, Long, Single, Double, or Decimal IntegerLong, Single, Double, or Decimal LongSingle, Double, or Decimal DecimalSingle, Double SingleDouble
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 codeNameDescription C or cCurrencyFormats with a dollar sign, commas and 2 decimal places. Negative values are enclosed in parentheses F or fFixed-pointFormats as a string of numeric digits, no commas, 2 decimal places and a minus sign for negative values. N or nNumberFormats with commas, 2 decimal places and a minus sign for negative values. D or dDigitsUse only for integer data types. Formats with a minus sign for negative values. P or pPercentageMultiplies 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’.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.