Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.

Similar presentations


Presentation on theme: "CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones."— Presentation transcript:

1 CH2 – Using Data

2 Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones int, double, char, string, bool 5, 1.8 x 10 4, 0x0101, abcde, T or F

3 Variable Declaration Statement which names a variable and reserves storage for it Data type Identifier – begins with lowercase letter Optional initial value Ending ; Assignment Operator = ‘is assigned the value of the following expression’ Initialization Assignment made when variable is declared int classPeriod = 4;

4 Turn to page 84 and answer Review Questions 1 – 3.

5 Turn to page 50, figures 2-1 and 2-2. discuss code and output Turn to page 51, figures 2-3 and 2-4 Discuss code and output Write () All output stays on the same line Format String Combination of strings and variable values into a single string

6 Placeholder – A pair of curly brackets Contains a number indicating the position of the variable The list follows the string First position is 0 May display multiple variables within a single call May display the same variable multiple times Console.WriteLine (“The money is ${0} exactly”, someMoney); Turn to page 52 and create the program on the bottom. Name it DisplayVariables Where do the numbers align in your output?

7 Alignment – Where on the line the characters are displayed Default = right Negative field value = left Field Size Length of output field Console.WriteLine (“{0, 5}”, num1); Turn to page 85 and answer Review Questions 4 -5. Turn to page 53 and create the program in the middle. Name it DisplayVariables2 Where do the numbers align in your output?

8 Integral Data Types Store whole numbers Byte, sbyte, short, ushort, int, uint, long, ulong, char Most basic is int Char – each letter has a numerical value to the computer int annualSalary = 20000;

9 Turn to page 75 and complete You Do It (YDI) # 1 – 8.

10 Floating-point Number Contains decimal positions Significant digits – mathematical accuracy of the value Float – holds as many as seven significant digits of accuracy Double – holds 15 or 16 significant digits of accuracy Decimal – greater precision than float and double; smaller range Floating-point number is double by default Place an F after the number to keep it floating-point float pocketChange = 4.87F;

11 C# displays floating-point numbers in the most concise way while maintaining the correct value. Double myMoney = 14.00; Will be displayed as 14 since the 00 do nothing for the value Standard numeric format strings Strings of characters Enclosed within “ “ Specify output format Format specifier Pg 57 Table 2-2 Precision specifier Specifies the number of digits to the right of the decimal moneyString = someMoney.ToString (“F”);

12 1. What does the 1 st WriteLine () display? 2. What does the 2 nd WriteLine () display? double someMoney = 123; string moneyString; moneyString = someMoney.ToString (“F”); Console.WriteLine (money.String); moneyString = someMoney.ToString (“F3”); Console.WriteLine (moneyString);

13 1. What does the 1 st WriteLine () display? 2. What does the 2 nd WriteLine () display? double someMoney = 456789; string conversion; conversion = someValue.ToString (“C”); Console.WriteLine (conversion); conversion = someValue.ToString (“C2”); Console.WriteLine (conversion);

14 Binary operators Two arguments One value on each side of the operator Operator + = add - = subtraction * = multiplication / = division % = remainder / operator Results is always an integer45/2 = 22 Remainder is dropped % operator Results is the number of digits in the remainder45/2 = 1 2 “goes into” 45 22 times with a remainder of 1

15 PEMDAS is important. Use() to clarify operations order

16 counter = counter +1; = is used to assign values “Take the value of counter, add 1 to it, and assign the results to counter.” Add and assign operator += Adds the operand on the right to the operand on the left assigns the results to the operand on the left one step Subtract and assign operator -= Multiple and assign operator *= Divide and assign operator /=

17 Unary operators Used with only one operand Used to increase or decrease a variable by 1 Prefix increment operator ++ in front of the variable name Results calculated 1 st and then stored int someValue = 6; ++someValue; Postfix increment operator ++ at the end of the variable name Variable used 1 st and calculation is stored int someValue = 6; someValue++; Prefix decrement operator and postfix decrement operator subtracts 1 from the variable  Worsk like the increment operator

18 Turn to page 85 and answer Review Questions 6 - 10. NEXT Turn to page 87 and answer Exercise #1, 3. NEXT Turn to page 76 – 77 and create YDI - Performing Arithmetic

19 Boolean Variable – T or F Comparison Operator Compares two items Returns a boolean value Turn to page 63, Table 2-4

20 Turn to page 86 and answer Review Questions 11 - 13. NEXT Turn to page 87 and answer Exercise #2. NEXT Turn to page 77 – 78 and create YDI - Working With Boolean Variables 1,2.

21 Unifying Type Happens when arithmetic operands have different types C# automatically converts to a unifying type The unifying type will have the highest ‘type preference’ This is called implicit cast. Turn to page 64 – The Implicit Numeric Conversions

22 Turn to page 86 and answer Review Questions 14 - 17.

23 char – holds any single character A number may be a character ‘ ‘ around the character char aNumValue = ‘9’; char aCharValue = ‘T’; Escape sequence – way to store nonprinting characters in the char variable. Inside ‘ ‘ Start with \ Turn to page 67, Table 2-5

24 string - holds a series of characters May compare string variables using == Returns a boolean value Turn to page 68, figure 2-11 and 2-12 String class Equals () Arguments within () Separate by a, Returns a boolean value Case sensitive Compare () Arguments within () Separate by a, Case sensitive Returns an integer 0 – equivalent +number – 1 st string > 2 nd string -number – 1 st string < 2 nd string Strings are compared left to right, letter by letter Turn to page 69, Figure 2-13 and 2-14.

25 Turn to page 86 and answer Review Questions 18, 19. NEXT Turn to page 87 and answer Exercise #4. NEXT Turn to page 78 - 79and create YDI - Using Escape Sequences

26 Constant value never changes Usually named with all UPPERCASE LETTERS const double TAX_RATE = 0.06; MUST assign a value when created

27 Interactive program Allows user input Console.ReadLine () myString = Console.ReadLine (); Using the string as a number Convert () Page 74 - Table 2-6

28 Turn to page 79 - 80 and create YDI – Writing a Program that Accepts User Input NEXT Turn to page 88 - 89 and create Ex. 6, 7, 12 -13 in the same program, 16. NEXT Go to the V drive and complete Debugging exercises a – d.


Download ppt "CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones."

Similar presentations


Ads by Google