Sunteți pe pagina 1din 44

Chapter Two

Using Data

1
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

2
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

3
Objectives

• Learn about the string data type


• Learn how to define named constants
• Learn how to accept console input

4
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

5
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

6
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

7
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

8
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;

9
Displaying Variable Values

• You can display variable values by using the


variable name within a WriteLine() method call

10
Displaying Variable Values

• Output of DisplaySomeMoney Program


11
Displaying Variable Values

• Program that displays a string and a variable value

12
Displaying Variable Values

• Output of DisplayMoney2 program

13
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);
14
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);

15
Displaying Variable Values

• If you use a second number within the curly braces in a


number format, you can specify alignment and field size
16
Displaying Variable Values

• Unaligned output

17
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;

18
Using the Standard Binary Arithmetic
Operators
• The five most commonly used binary arithmetic
operators

19
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

20
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 (--)

21
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

22
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

23
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

24
Formatting Floating-Point Values

• Format Specifiers

25
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

26
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

27
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

28
Using the char Data Type

• Common Escape Sequences

29
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

30
Using the string Data Type

• Program that compares two strings using ==


31
Using the string Data Type

• Output of CompareNames1 program that uses ==


32
Using the string Data Type

• Program that compares two strings using Equals() and


Compares()
33
Using the string Data Type

• Output of CompareNames2 program that uses Equals()


and Compare()
34
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

35
Defining Named Constants

• SalesTax program

36
Defining Named Constants

• Output of SalesTax program

37
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

38
Accepting Console Input

• InteractiveSalesTax program

39
Accepting Console Input

• Output of InteractiveSalesTax program

40
Accepting Console Input

• Typical run of InteractiveAddition program


41
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

42
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

43
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

44

S-ar putea să vă placă și