Top Banner
Microsoft Visual C# 2010 Fourth Edition Chapter 2 Using Data
41
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Csc153 chapter 02

Microsoft Visual C# 2010Fourth Edition

Chapter 2Using Data

Page 2: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 22

Objectives

• Learn about declaring variables

• Display variable values

• Learn about the integral data types

• Learn about floating-point data types

• Use arithmetic operators

Page 3: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 33

Objectives (cont'd.)

• Learn about the bool data type

• Learn about numeric type conversion

• Learn about the char data type

• Learn about the string data type

• Define named constants and enumerations

• Accept console input

Page 4: Csc153 chapter 02

Declaring Variables

• Constant– Cannot be changed after a program is compiled

• Literal constant– Its value is taken literally at each use

• Variable– A named location in computer memory that can hold

different values at different points in time

• Data type– Describes the format and size of (amount of memory

occupied by) a data item

Microsoft Visual C# 2010, Fourth Edition 44

Page 5: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 55

Page 6: Csc153 chapter 02

Declaring Variables (cont'd.)

• Variable declaration– 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 commaMicrosoft Visual C# 2010, Fourth Edition 66

Page 7: Csc153 chapter 02

Displaying Variable Values

Microsoft Visual C# 2010, Fourth Edition 77

Page 8: Csc153 chapter 02

Displaying Variable Values (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 88

Page 9: Csc153 chapter 02

Displaying Variable Values (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 99

Page 10: Csc153 chapter 02

Displaying Variable Values (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 1010

Page 11: Csc153 chapter 02

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

Microsoft Visual C# 2010, Fourth Edition 1111

Page 12: Csc153 chapter 02

Displaying Variable Values (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 1212

Page 13: Csc153 chapter 02

Displaying Variable Values (cont'd.)

• Formatting outputint num1 = 4, num2 = 56, num3 = 789;

Console.WriteLine(“{0, 5}”, num1);

Console.WriteLine(“{0, 5}”, num2);

Console.WriteLine(“{0, 5}”, num3);

• Concatenate strings– Using the plus (+) sign

Microsoft Visual C# 2010, Fourth Edition 1313

Page 14: Csc153 chapter 02

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)

Microsoft Visual C# 2010, Fourth Edition 1414

Page 15: Csc153 chapter 02

Using Floating-Point Data Types

• Floating-point number– Contains decimal positions

• Floating-point data types– float

• Can hold up to seven significant digits of accuracy

– double• Can hold 15 or 16 significant digits of accuracy

– decimal• Has a greater precision and a smaller range

• Suitable for financial and monetary calculations

Microsoft Visual C# 2010, Fourth Edition 1515

Page 16: Csc153 chapter 02

Using Floating-Point Data Types (cont'd.)

• Significant digits– Specifies the mathematical accuracy of the value

• Suffixes – Put an F after a number to make it a float– Put a D after it to make it a double– Put an M after it to make it a decimal

• Scientific notation– Includes an E (for exponent)

Microsoft Visual C# 2010, Fourth Edition 1616

Page 17: Csc153 chapter 02

Formatting Floating-Point Values

• C# displays floating-point numbers in the most concise way it can– While maintaining the correct value

• Standard numeric format strings– Strings of characters expressed within double

quotation marks that indicate a format for output– Take the form X0

• X is the format specifier; 0 is the precision specifier

• Format specifiers– Define the most commonly used numeric format

typesMicrosoft Visual C# 2010, Fourth Edition 1717

Page 18: Csc153 chapter 02

Formatting Floating-Point Values (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 1818

Page 19: Csc153 chapter 02

Using the Standard Binary 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 always take

place prior to addition or subtraction in an expression– You can override normal operator precedence with

parentheses

Microsoft Visual C# 2010, Fourth Edition 1919

Page 20: Csc153 chapter 02

Using the Standard Binary Arithmetic Operators (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 2020

Page 21: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 2121

Using Shortcut Arithmetic Operators

• Add and assign operator– Example: bankBal += bankBal * interestRate;– Variations: –=, *=, and /=

• Prefix increment operator– Example: ++someValue;

• Postfix increment operator– Example: someValue++;

• Unary operator– Use only one value

• Decrement operator (--)

Page 22: Csc153 chapter 02

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 has

a Boolean value

Microsoft Visual C# 2010, Fourth Edition 2222

Page 23: Csc153 chapter 02

Using the bool Data Type (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 2323

Page 24: Csc153 chapter 02

Understanding Numeric Type Conversion

• Arithmetic with variables or constants of the same type– Result retains the same type

• Arithmetic with operands of dissimilar types– C# chooses a unifying type for the result– Implicitly (or automatically) converts nonconforming

operands to the unifying type• Type with the higher type precedence

Microsoft Visual C# 2010, Fourth Edition 2424

Page 25: Csc153 chapter 02

Understanding Numeric Type Conversion (cont'd.)

• Implicit cast– Automatic transformation that occurs when a value is

assigned to a type with higher precedence

• Explicit cast– Placing the desired result type in parentheses

• Followed by the variable or constant to be cast

Microsoft Visual C# 2010, Fourth Edition 2525

Page 26: Csc153 chapter 02

Using the char Data Type

• char data type– Holds any single character

• Place constant character values within single quotation marks

• Escape sequence– Stores a pair of characters– Begins with a backslash– Pair of symbols represents a single character

Microsoft Visual C# 2010, Fourth Edition 2626

Page 27: Csc153 chapter 02

Using the char Data Type (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 2727

Page 28: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 2828

Using the string Data Type

• string data type– Holds a series of characters

• Values are expressed within double quotation marks

• Comparing strings– Use == and !=– Methods Equals(), Compare(), CompareTo()

Page 29: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 2929

Using the string Data Type (cont'd.)

Page 30: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 3030

Using the string Data Type (cont'd.)

Page 31: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 31

Using the string Data Type (cont'd.)

• Use the length property of a string to determine its length– The length of “water” is 5

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

Page 32: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 32

Using the string Data Type (cont'd.)

Page 33: Csc153 chapter 02

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

Microsoft Visual C# 2010, Fourth Edition 3333

Page 34: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 34

Working with Enumerations

• An enumeration is a set of constants represented by identifiers

• The following is an enumeration called DayOfWeek:enum DayOfWeek

{

SUNDAY, MONDAY, TUESDAY, WEDNESDAY

THURSDAY, FRIDAY, SATURDAY

}

Page 35: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 35

Working with Enumerations (cont'd.)

• By default, enumeration values are integers– Can specify otherwise by including a colon and a

type name after the enumeration name

• The identifiers in an enumeration are often meant to hold consecutive values– When you don’t supply values, they start at 0 and

increment by 1– In the DayOfWeek enumeration, SUNDAY is 0, MONDAY is 1, and so on

Page 36: Csc153 chapter 02

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

Microsoft Visual C# 2010, Fourth Edition 3636

Page 37: Csc153 chapter 02

Accepting Console Input (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 3737

Page 38: Csc153 chapter 02

Accepting Console Input (cont'd.)

Microsoft Visual C# 2010, Fourth Edition 3838

Page 39: Csc153 chapter 02

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

Microsoft Visual C# 2010, Fourth Edition 3939

Page 40: Csc153 chapter 02

Summary

• Constant: cannot be changed after compilation

• Can display variable values with Write() or WriteLine()

• Nine integral data types: byte, sbyte, short, ushort, int, uint, long, ulong, and char

• Three floating-point data types: float, double, and decimal

• Use the binary arithmetic operators +, –, *, /, and % to manipulate values in your programs

• Shortcut arithmetic operators

Microsoft Visual C# 2010, Fourth Edition 4040

Page 41: Csc153 chapter 02

Microsoft Visual C# 2010, Fourth Edition 4141

Summary (cont'd.)

• A bool variable can be true or false

• Implicit cast versus explicit cast• char data type holds any single character• string data type holds a series of characters

• Named constants are program identifiers whose values cannot change.

• Console.ReadLine() method accepts user input