44
1 Chapter Two Using Data

1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

Embed Size (px)

Citation preview

Page 1: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

1

Chapter Two

Using Data

Page 2: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

2

Objectives

• Learn about variable types and how to declare variables

• Learn how to display variable values• Learn about the integral data types• Learn how to use standard binary arithmetic

operators• Learn how to use shortcut arithmetic operators

Page 3: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

3

Objectives

• Learn about the Boolean data type• Learn about floating-point data types• Learn how to format floating-point values• Learn about numeric type conversion• Learn about the char data type

Page 4: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

4

Objectives

• Learn about the string data type• Learn how to define named constants• Learn how to accept console input

Page 5: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

5

Declaring Variables

• A data item is classified as either constant or variable• Constant data items cannot vary• Variable data items can hold different values at different

points of time• All data items in a C# program have a data type• Data type describes the format and size of a piece of

data

Page 6: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

6

Declaring Variables

• C# provides 14 basic or intrinsic types • The most commonly used data types are: int,

double, char, string, and bool• Each C# intrinsic type is an alias, or other name

for, a class in the System namespace

Page 7: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

7

Declaring Variables

• A variable declaration includes:– The data type that the variable will store– An identifier that is the variable’s name– An optional assignment operator and

assigned value when you want a variable to contain an initial value

– An ending semicolon

Page 8: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

8

Declaring Variables

• Examples of variable declarations:int myAge = 25;

• The equal sign (=) is an assignment operator• The assignment operator works from right to left• An assignment made when a variable is declared is an

initialization• It is not necessary to initialize a variable during a

declaration. For example int myAge; is a valid declaration

Page 9: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

9

Declaring Variables

• Examples of multiple variable declarations:– int myAge = 25;

int yourAge = 19;– int myAge = 25, your Age = 19;

• Declarations of variables of the same type can be separated by commas. However, separate statements are used when declaring variables of different types– int myAge, yourAge;

double mySalary, yourSalary;

Page 10: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

10

Displaying Variable Values

• You can display variable values by using the variable name within a WriteLine() method call

Page 11: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

11

Displaying Variable Values

• Output of DisplaySomeMoney Program

Page 12: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

12

Displaying Variable Values

• Program that displays a string and a variable value

Page 13: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

13

Displaying Variable Values

• Output of DisplayMoney2 program

Page 14: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

14

Displaying Variable Values

• A format string is a string of characters that contains one or more placeholders. For example:– Console.WriteLine(“The money is {0} exactly”, someMoney);

Page 15: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

15

Displaying Variable Values

• The number within the curly braces in the format string must be less than the number of values you list after the format string

• You do not have to use the positions in order:– Console.WriteLine

(“The money is {0} . ${0} is a lot for my age which is {1}”, someMoney, myAge);

Page 16: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

16

Displaying Variable Values

• If you use a second number within the curly braces in a number format, you can specify alignment and field size

Page 17: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

17

Displaying Variable Values

• Unaligned output

Page 18: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

18

Using the Integral Data Types

• In C#, there are nine integral data types: byte, sbyte, short, ushort, int, uint, long, ulong, and char

• char is used for characters like ‘A’ or ‘b’, while the other eight are used to store whole numbers

• You use variables of type int to store integers• When you assign a value to a numeric variable, you do

not type any commas:– int cost = 1000; // NOT int cost = 1,000;

Page 19: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

19

Using the Standard Binary Arithmetic Operators

• The five most commonly used binary arithmetic operators

Page 20: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

20

Using the Standard Binary Arithmetic Operators

• When you divide two integers (using the division operator or the modulus operator), the result is ALWAYS an integer

• Operator precedence refers to the order in which parts of a mathematical expression are evaluated

Page 21: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

21

Using Shortcut Arithmetic Operators

• C# provides you with several shortcut ways to count and accumulate– Examples of Shortcuts:

+=, -+, *=, /=

• The prefix and postfix operators can also provide a shorthand way of writing operators– Examples of the unary operators:

++val, val++

• Besides the prefix and postfix increment operators, you can also use a decrement operator (--)

Page 22: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

22

Using the Boolean Data Type

• Boolean logic is based on true or false comparisons• Boolean variables can hold only one of two values:

true or false• You can assign values based on the result of

comparisons to Boolean variables

Page 23: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

23

Using Floating-Point Data Types

• A floating-point number is one that contains decimal positions

• C# supports three floating-point data types: float, double, and decimal

• A double can hold 15 to 16 significant digits of accuracy

• Floating-point numbers are double by default

Page 24: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

24

Formatting Floating-Point Values

• Standard numeric format strings are strings of characters expressed within double quotation marks that indicate a format for output. For example, “F3” , where F is the format specifier and 3 is the precision specifier, indicates a fixed point with three significant digits.

• The format specifier can be one of nine built-in format characters

• The precision specifier controls the number of significant digits or zeros to the right of the decimal point

Page 25: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

25

Formatting Floating-Point Values

• Format Specifiers

Page 26: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

26

Understanding Numeric Type Conversion

• In arithmetic operations with variables or constants of the same type, the result of the operation usually retains the same type

- For example, an integer type added to another integer type will result in an integer type

• When an arithmetic operation is performed with dissimilar types, C# chooses a unifying type for the result and implicitly converts the nonconforming operands to the unifying type

• There are rules followed by C# for implicit numeric conversions

Page 27: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

27

Understanding Numeric Type Conversion

• When you perform a cast, you explicitly override the unifying type imposed by C#

• For example:

double balance = 189.66;

int dollars = (int)balance;• A cast involves placing the desired resulting type in

parentheses followed by the variable or constant to be cast

Page 28: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

28

Using the char Data Type

• The char data type is used to hold a single character (‘A’,’b’,’9’)

• A number can be a character only if it is enclosed in a single quotation mark (char aChar = 9; is illegal)

• You can store any character in a char variable, including escape sequences

• Characters in C# are represented in Unicode

Page 29: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

29

Using the char Data Type

• Common Escape Sequences

Page 30: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

30

Using the string Data Type

• In C# the string data type holds a series of characters• Although you can use the == comparison operator with

strings that are assigned literal values, you CANNOT use it with strings created through other methods

• C# recommends that you use the Equals() and Compare() methods to compare strings

• A string is considered greater than another string when it is greater lexically

Page 31: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

31

Using the string Data Type

• Program that compares two strings using ==

Page 32: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

32

Using the string Data Type

• Output of CompareNames1 program that uses ==

Page 33: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

33

Using the string Data Type

• Program that compares two strings using Equals() and Compares()

Page 34: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

34

Using the string Data Type

• Output of CompareNames2 program that uses Equals() and Compare()

Page 35: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

35

Defining Named Constants

• A named constant is an identifier whose contents cannot change

• You create a named constant using the keyword const• Programmers usually name constants using all

uppercase letters, inserting underscores for readability• Often times, constant statements are self-documenting

Page 36: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

36

Defining Named Constants

• SalesTax program

Page 37: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

37

Defining Named Constants

• Output of SalesTax program

Page 38: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

38

Accepting Console Input

• A program that allows user input is an interactive program

• The Console.ReadLine() method accepts user input from the keyboard

• This method accepts a user’s input as a string• You must use a Convert() method to convert the input

string to the type required

Page 39: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

39

Accepting Console Input

• InteractiveSalesTax program

Page 40: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

40

Accepting Console Input

• Output of InteractiveSalesTax program

Page 41: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

41

Accepting Console Input

• Typical run of InteractiveAddition program

Page 42: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

42

Chapter Summary

• Data is constant when it cannot be changed after a program is compiled

• You can display variable values by using the variable name within a WriteLine() or Write() method call

• In C#, nine data types are considered integral data types• Use the binary arithmetic operators to manipulate values

in your program• Because increasing the value of a variable is a common

task, C# provides you with several shortcut arithmetic operators

Page 43: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

43

Chapter Summary

• A Boolean variable can hold only one of two values: true or false

• C# supports three floating-point data types• When you perform arithmetic with variables or constants

of the same type, the result of the arithmetic retains the same type. When you perform arithmetic operations with operands of different types, C# chooses a unifying type for the result and implicitly converts nonconforming operands to the unifying type

Page 44: 1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral

44

Chapter Summary

• Use the char data type to hold any single character• Use the string data type to hold a series of characters• Named constants are program identifiers you cannot

change• Use the Console.ReadLine() method to accept user input