# Variables

-a variable is a named memory location which stores a value

--to use a variable in a program, it must be defined by giving it a type and identifier

eg: double Radius

Once defined the variable must be given a value

eg: Radius = 12.3 ;
the = sign is similar to the := in Pascal which means that the value is assigned to the variable

Using Named Constants

-defined in a statement and begins with the word "const"

--this is used to define a constant: in math Pi is a constant value (3.14159)...to represent this in C++, you do the following:

const double PI = 3.14 ;

--a constant is used to represent a value that does not change during the execution of a program

Choosing Identifiers

Rules for choosing identifiers:
• must start with a letter
• must contain only letters, digits and the underscore ( _ ) character
• can be of any length
• are case sensitive: Radius is different than radius

Built in Data Types

1. double
• used to store positive or negative real numbers (decimals)
• floating point numbers (decimals)
• when the number is very large, it will be stored in scientific notation: the computer stores 1.23457 x 108 as 1.23457e+08
• the double value is rounded to six digits

2. int and long

• used to store positive and negative integers (no decimals!)
• only difference is the range of values : long goes beyond 32767

3. char:

• used to store a single character
• can be used to store a single letter, digit or symbol like the % sign
• requires single quotation marks:

Example: char Ch:

Ch = 'A';

cout<<Ch<<endl;

displays : A

VARIABLE DEFINITIONS

VARIABLES can be defined in single statements or in multiple variables in one line

example: int x,y,z

Char ch1,ch2

Expressions and Operators

-built in arithmetic operations include: (*) multiplication,

(+) addition, (-) subtraction, and ( / ) division

Promotion: numeric values of various types can be mixed in an expression: an expression involving two doubles, results in a double, however, whenever an integer and a double are used, C++ promotes the value with the narrow range to the value with the wider range: integer becomes a double: this avoids the problem known in Pascal as "type mismatch"

Integer Division: if you divide two integers, the resulting number will be truncated, or the decimals will be cut off!

Type Casting: when real division is done, type casting converts one of the values into a real number

Modulus Division: represented by %, Modulus division returns the remainder....similar to MOD in Pascal

Operator Precedence: same as order of operations in mathematics

Output Formatting

• use width ( ) to control your output: creates a field width, so width(10) will allow 10 digits
• use setf( ) to change the alignment (see handout)
• use precision ( ) to specify the number of decimals

Defining Variables and Type Casting: More Details  