Monday, September 29, 2008

Data Types

Here is the kind of Data types, able use in C++

  • Unsigned Char
  • Char
  • Enumeration
  • Unsigned Int
  • Short Int
  • Int
  • Unsigned Long
  • Long
  • Float
  • Double
  • Long Double

Declaration of variables

In order to use a variable in C++, we must first declare it specifying which of the data types above we want it to be. The syntax to declare a new variable is to write the data type specifier that we want (like int, short, float...) followed by a valid variable identifier. For example:

int a;
float mynumber;

Operator

Arithmetic operators ( +, -, *, /, % )

The five arithmetical operations supported by the language are:

+

addition

-

subtraction

*

multiplication

/

division

%

module

Compound assignation operators (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=)

value += increase; is equivalent to value = value + increase;

a -= 5; is equivalent to a = a - 5;

a /= b; is equivalent to a = a / b;

price *= units + 1; is equivalent to price = price * (units + 1);

Example:

#include

void main()

{

int number;

clrscr();

number=10;

cout<<“Number before given any operator symbol: “<<number;

number+=5;

cout<<“\nThe result number +=5 : “<<number;

number-=10;

cout<<“\nThe result number -=10 : “<<number;

number*=10;

cout<<“\nThe result number *=10 : “<<number;

getch();

}

If above program is execution will appear result that shown in below:

Number before given any operator symbol: 10

The result number +=5 : 15

The result number -=10 : 5

The result number *=10 : 50

Relational operators ( ==, !=, >>, <, >=, <= )

In order to evaluate a comparison between two expressions we can use the Relational operators. As specified by the ANSI-C++ standard, the result of a relational operation is a Boolean value that can only be true or false, according to the result of the comparison.

We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other. Here is a list of the relational operators that can be performed in C++:

== Equal

!= Different

&gt; Greater than

< Less than

<= Greater or equal than

>= Less or equal than

Example:

(7 == 5) would return false.

(5 > 4) would return true.

Logic operators ( !, &&&, || ).

The Operator of ! is equivalent to boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to invert the value of it, producing false if its operand is true and true if its operand is false. It is like saying that it returns the opposite result of evaluating its operand. For example:

!(5 == 5) returns false

Logic operators && and || are used when evaluating two expressions to obtain a single result. They correspond with boolean logic operations AND and OR respectively. The result of them depends on the relation between its two operands:

A

B

A && B

A || B

True

True

True

True

True

False

False

True

False

True

False

True

False

False

False

False


Source: cplusplus.com



No comments: