Chapter 2 using data
This presentation is the property of its rightful owner.
Sponsored Links
1 / 35

Chapter 2: Using Data PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 2: Using Data. 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. Declaring Variables (cont’d.). Variable declaration

Download Presentation

Chapter 2: Using Data

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 2 using data

Chapter 2:

Using Data


Declaring 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

Microsoft Visual C# 2012, Fifth Edition


Chapter 2 using data

Microsoft Visual C# 2012, Fifth Edition


Declaring variables cont d

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

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values

Displaying Variable Values

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d1

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d2

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d3

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

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d4

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d5

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d6

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");

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Displaying variable values cont d7

Displaying Variable Values (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Using the integral data types

Integraldata 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)

Using the Integral Data Types

Microsoft Visual C# 2012, Fifth Edition


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

Using Floating-Point Data Types

Microsoft Visual C# 2012, Fifth Edition


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

Using Arithmetic Operators

Microsoft Visual C# 2012, Fifth Edition


Using arithmetic operators cont d

Using Arithmetic Operators (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Using shortcut arithmetic operators

Add and assign operator

Example: bankBal += bankBal * interestRate;

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

Prefix increment operator

Example: ++someValue;

Postfix increment operator

Example: someValue++;

Using Shortcut Arithmetic Operators

Microsoft Visual C# 2012, Fifth Edition


Using shortcut arithmetic operators cont d

Using Shortcut Arithmetic Operators (cont’d.)

19

Microsoft Visual C# 2012, Fifth Edition

  • Unary operator

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

  • Decrement operator (--)

    • Can be prefix or postfix


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

Using the bool Data Type

Microsoft Visual C# 2012, Fifth Edition


Using the bool data type cont d

Using the bool Data Type (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Using the bool data type cont d1

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

}

Microsoft Visual C# 2012, Fifth Edition


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;

Understanding Numeric Type Conversion

Microsoft Visual C# 2012, Fifth Edition


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

Using the char Data Type

Microsoft Visual C# 2012, Fifth Edition


Using the char data type cont d

Using the char Data Type (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


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

Using the string Data Type

Microsoft Visual C# 2012, Fifth Edition


Using the string data type cont d

Using the string Data Type (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Chapter 2 using data

Microsoft Visual C# 2012, Fifth Edition


Using the string data type cont d1

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

Using the string Data Type (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Using the string data type cont d2

Using the string Data Type (cont’d.)

Microsoft Visual C# 2012, Fifth Edition

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


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

Defining Named Constants

Microsoft Visual C# 2012, Fifth Edition


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

Accepting Console Input

Microsoft Visual C# 2012, Fifth Edition


Accepting console input cont d

Accepting Console Input (cont’d.)

Microsoft Visual C# 2012, Fifth Edition


Chapter 2 using data

Microsoft Visual C# 2012, Fifth Edition


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

You Do It

Microsoft Visual C# 2012, Fifth Edition


  • Login