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 int, double, char, string, bool 5, 1.8 x 10 4, 0x0101, abcde, T or F
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;
Turn to page 84 and answer Review Questions 1 – 3.
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
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?
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 Turn to page 53 and create the program in the middle. Name it DisplayVariables2 Where do the numbers align in your output?
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;
Turn to page 75 and complete You Do It (YDI) # 1 – 8.
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;
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”);
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);
1. What does the 1 st WriteLine () display? 2. What does the 2 nd WriteLine () display? double someMoney = ; string conversion; conversion = someValue.ToString (“C”); Console.WriteLine (conversion); conversion = someValue.ToString (“C2”); Console.WriteLine (conversion);
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” times with a remainder of 1
PEMDAS is important. Use() to clarify operations order
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 /=
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
Turn to page 85 and answer Review Questions NEXT Turn to page 87 and answer Exercise #1, 3. NEXT Turn to page 76 – 77 and create YDI - Performing Arithmetic
Boolean Variable – T or F Comparison Operator Compares two items Returns a boolean value Turn to page 63, Table 2-4
Turn to page 86 and answer Review Questions NEXT Turn to page 87 and answer Exercise #2. NEXT Turn to page 77 – 78 and create YDI - Working With Boolean Variables 1,2.
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
Turn to page 86 and answer Review Questions
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
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.
Turn to page 86 and answer Review Questions 18, 19. NEXT Turn to page 87 and answer Exercise #4. NEXT Turn to page and create YDI - Using Escape Sequences
Constant value never changes Usually named with all UPPERCASE LETTERS const double TAX_RATE = 0.06; MUST assign a value when created
Interactive program Allows user input Console.ReadLine () myString = Console.ReadLine (); Using the string as a number Convert () Page 74 - Table 2-6
Turn to page and create YDI – Writing a Program that Accepts User Input NEXT Turn to page and create Ex. 6, 7, in the same program, 16. NEXT Go to the V drive and complete Debugging exercises a – d.