Rabu, 22 Juli 2009

Variables and Arithmetics

Every name and every expression has a type that determines the operations that may be performed on it. For example,
the declaration :
int inch ;
specifies that inch is of type int ; that is, inch is an integer variable.

A declaration is a statement that introduces a name into the program. It specifies a type for that name. A type defines the proper use of a name or an expression. C++ offers a variety of fundamental types, which correspond directly to hardware facilities. For example:
bool // Boolean, possible values are true and false
char // Character, for example, ’a’, ’z’, and ’9’
int // Integer, for example, 1, 42, and 1216
double // Doubleprecision floatingpoint for example, 3.14 and 29.0

A char variable is of the natural size to hold a character on a given machine (typically a byte), and an int variable is of the natural size for integer arithmetic on a given machine (typically a word).

The arithmetic operators can be used for any combination of these types:
+ // plus, both unary and binary
- // minus, both unary and binary
* // multiply
/ // divide
% // remainder

So can the comparison operators:
== // equal
!= // not equal
< // less than > // greater than
<= // less than or equal >= // greater than or equal

In assignments and in arithmetic operations, C++ performs all meaningful conversions between the basic types so that they can be mixed freely:

void some _ function () // function that doesn’t return a value
{
double d = 2 .2 ; // initialize floatingpoint number
int i = 7 ; // initialize integer
d = d +i ; // assign sum to d
i = d *i ; // assign product to i
}

As in C, = is the assignment operator and == tests equality.

0 komentar:

Posting Komentar