chapter 2: using data · using the integral data types •integral data types –types that store...

35
Chapter 2: Using Data

Upload: others

Post on 21-Jun-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Chapter 2:

Using Data

Page 2: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Declaring Variables

• Constant

– Cannot be changed after a program is compiled

• Variable

– A named location in computer memory that can hold different values at different points in time

2 Microsoft Visual C# 2012, Fifth Edition

Page 3: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

3 Microsoft Visual C# 2012, Fifth Edition

Page 4: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Declaring Variables (cont’d.)

• Variable declaration

– A statement that names a variable and reserves storage

– Example: int myAge = 25;

• You can declare multiple variables of the same type

– In separate statements on different lines

• You can declare two variables of the same type in a single statement

– By using the type once and separating the variable declarations with a comma

4 Microsoft Visual C# 2012, Fifth Edition

Page 5: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values

5 Microsoft Visual C# 2012, Fifth Edition

Page 6: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

6 Microsoft Visual C# 2012, Fifth Edition

Page 7: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

7 Microsoft Visual C# 2012, Fifth Edition

Page 8: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

8 Microsoft Visual C# 2012, Fifth Edition

Page 9: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

• Format string

– A string of characters that optionally contains fixed text

– Contains one or more format items or placeholders for variable values

• Placeholder

– Consists of a pair of curly braces containing a number that indicates the desired variable’s position in a list that follows the string

9 Microsoft Visual C# 2012, Fifth Edition

Page 10: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

10 Microsoft Visual C# 2012, Fifth Edition

Page 11: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

11 Microsoft Visual C# 2012, Fifth Edition

Page 12: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

• Formatting output Console.WriteLine("{0, 5}", 4);

Console.WriteLine("{0, 5}", 56);

Console.WriteLine("{0, 5}", 789);

Console.WriteLine("{0, -8}{1, -8}", "Richard", "Lee");

Console.WriteLine("{0, -8}{1, -8}", "Marcia", "Parker");

Console.WriteLine("{0, -8}{1, -8}", "Ed", "Tompkins");

• Concatenate strings using the plus (+) sign Console.WriteLine("Concatenated " + "string");

12 Microsoft Visual C# 2012, Fifth Edition

Page 13: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Displaying Variable Values (cont’d.)

13 Microsoft Visual C# 2012, Fifth Edition

Page 14: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the Integral Data Types

• Integral data types

– Types that store whole numbers

– byte, sbyte, short, ushort, int, uint, long, ulong, and char

• Variables of type int

– Store (or hold) integers, or whole numbers

• Shorter integer types – byte, sbyte (which stands for signed byte), short

(short int), or ushort (unsigned short int)

14 Microsoft Visual C# 2012, Fifth Edition

Page 15: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using Floating-Point Data Types

• Floating-point number

– Contains decimal positions

• Floating-point data types – float

• Can hold up to 7 significant digits of accuracy

– double

• Can hold 15 or 16 significant digits of accuracy

– decimal

• Can hold 29 significant digits but a smaller range

• Suitable for financial and monetary calculations

15 Microsoft Visual C# 2012, Fifth Edition

Page 16: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using Arithmetic Operators

• Binary operators

– Use two values (operands)

• Operator precedence

– Rules that determine the order in which parts of a mathematical expression are evaluated

– Multiplication, division, and remainder (modulus) always take place prior to addition or subtraction in an expression

– You can override normal operator precedence with parentheses

– Expressions are evaluated left to right

16 Microsoft Visual C# 2012, Fifth Edition

Page 17: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using Arithmetic Operators (cont’d.)

17 Microsoft Visual C# 2012, Fifth Edition

Page 18: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using Shortcut Arithmetic Operators

• Add and assign operator

– Example: bankBal += bankBal * interestRate;

– Variations: –=, *=, /=, and %=

• Prefix increment operator – Example: ++someValue;

• Postfix increment operator – Example: someValue++;

18 Microsoft Visual C# 2012, Fifth Edition

Page 19: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Microsoft Visual C# 2012, Fifth Edition

Using Shortcut Arithmetic Operators (cont’d.)

• Unary operator

– Uses only one value (e.g., –123)

• Decrement operator (--)

– Can be prefix or postfix

19 19

Page 20: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the bool Data Type

• Boolean variable

– Can hold only one of two values—true or false

– Declare a Boolean variable with type bool

• Comparison operator

– Compares two items

– An expression containing a comparison operator returns a Boolean value

20 Microsoft Visual C# 2012, Fifth Edition

Page 21: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the bool Data Type (cont’d.)

21 Microsoft Visual C# 2012, Fifth Edition

Page 22: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the bool Data Type (cont’d.)

// Example using a bool data type and a comparison

// operator

bool employeeWorkedOvertime = hoursWorked > 40;

if (employeeWorkedOvertime)

{

// Code to calculate overtime goes here

}

22 Microsoft Visual C# 2012, Fifth Edition

Page 23: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Understanding Numeric Type Conversion

• Implicit cast

– Automatic transformation that occurs when a value is assigned to a type with higher precedence

– Example: aDouble = anInt;

• Explicit cast

– Placing the desired result type in parentheses followed by the variable or constant to be cast

– Example: anInt = (int)aDouble;

23 Microsoft Visual C# 2012, Fifth Edition

Page 24: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the char Data Type

• char data type

– Holds any single Unicode character

• Place constant character values within single quotation marks (e.g., 'A')

• Escape sequence

– Stores a pair of characters

– Begins with a backslash

– A pair of symbols represents a single character

24 Microsoft Visual C# 2012, Fifth Edition

Page 25: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the char Data Type (cont’d.)

25 Microsoft Visual C# 2012, Fifth Edition

Page 26: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the string Data Type

• string data type

– Holds a series of characters

• Values are expressed within double quotation marks

• Comparing strings – Use == and !=

– Use the Equals() method, Compare() method, and CompareTo() method

26 Microsoft Visual C# 2012, Fifth Edition

Page 27: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the string Data Type (cont’d.)

27 Microsoft Visual C# 2012, Fifth Edition

Page 28: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

28 Microsoft Visual C# 2012, Fifth Edition

Page 29: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Using the string Data Type (cont’d.)

• Use the Length property of a string to determine its length

– Example: string aString = "How long is this string?";

Console.WriteLine("{0} It is {1} characters long.",

aString, aString.Length);

29 Microsoft Visual C# 2012, Fifth Edition

Page 30: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

• Use the Substring() method to extract a portion of a string from a starting point for a specific length

30 Microsoft Visual C# 2012, Fifth Edition

Using the string Data Type (cont’d.)

Page 31: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Defining Named Constants

• Named constant

– Often simply called a constant

– An identifier whose contents cannot change

– Created using the keyword const

• Programmers usually name constants using all uppercase letters, inserting underscores for readability

• Self-documenting statement

– Easy to understand even without program comments

31 Microsoft Visual C# 2012, Fifth Edition

Page 32: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Accepting Console Input

• Interactive program

– A program that allows user input

• Console.ReadLine() method

– Accepts user input from the keyboard

– Accepts all of the characters entered by a user until the user presses Enter

– Characters can be assigned to a string

– Must use a conversion method to convert the input string to the proper type

32 Microsoft Visual C# 2012, Fifth Edition

Page 33: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

Accepting Console Input (cont’d.)

33 Microsoft Visual C# 2012, Fifth Edition

Page 34: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

34 Microsoft Visual C# 2012, Fifth Edition

Page 35: Chapter 2: Using Data · Using the Integral Data Types •Integral data types –Types that store whole numbers –byte, sbyte, short, ushort, int, uint, long, ulong, and char •Variables

You Do It

• Activities to explore:

– Declaring and Using Variables

– Performing Arithmetic

– Working with Boolean Variables

– Using Escape Sequences

– Writing a Program that Accepts User Input

35 Microsoft Visual C# 2012, Fifth Edition