__Built-in data types__- Integers (size : 2 bytes)

eg : int age = 5 ; - Float numbers (size : 4 bytes)

eg : float weight = 84.5 ; - Characters (size : 1 byte)

eg : char grade = 'b' ; - Double float numbers (size : 8 bytes)

eg : double distance = 33.333 ;

- Integers (size : 2 bytes)
__Data constructions____Arrays__- int list[3] ;

char table[4][3] ;

list[2] = 333

table[1][3] = 'c' ;- List is a one dimension array ( 3 items )of integers.
- table is a two dimensions array ( 4 rows and 3 columns )of characters.
- the value of the third element ( notice that it starts from 0 ) is '333'.
- the value of the fourth row in the third column is 'c'.

- int list[3] ;
__Pointers__- int y , * x ;

y = 3 ;

x = & y ;

* x ++- We first initialized two variables ( y is an integer and x is a pointer to an integer )
- we assigned ( y = 3 )
- We let x points to y ( put the address of y in x )
- we increment the variable which the x is pointing to ( y = 4 now )

- int y , * x ;
__Dynamic allocaton__- These variables are created when we need'em and then they must
be deleted.

notice that these variables are generated during the excution time not in the compiling time as the other variables. - Creating a new variable :

eg : int * xPtr = new int ;

eg : char * NamePtr = new char[17] ; - Deleting it :

eg : delete xPtr;

eg : delete [17] NamePtr ;

- These variables are created when we need'em and then they must
be deleted.
__Structures__- Defining a structure :

sruct employee {

int id;

float salary;

char phone[30];

};- Here we've defined a new variable type called employee

- Declaring a structure :

struct employee Hussein,Ali;- Here we've declared two variables called 'Hussein' and 'Ali' respectively of type employee

- Accessing an element of the structure :

Hussein.id = 5 ;

cout << " Ali's salary = " << Ali.salary ;- Here we've put the value '5' in the 'id' of the 'Hussein' , and then we displayed the 'salary' of 'Ali'

- Pointers to structures
- Assume we've the structure written in the previous example.
- Declaring a pointer to it :

employee * pt = & Ali; - Accessing one of it's elements:

pt -> salary = 22.3 ;

or :

(*pt).salary = 22.3 ;

- Defining a structure :

__Operators____Arithmatic__

( + ) Addition.

assume we have x = 5 .- x = x + 3 ;

x now equals 8 - cout << + + x ;

The computer will display the number 6 - cout << x + + ;

The computer will dispaly the number 5

( - ) Subtraction.

assume we have x = 5 .- x = x - 3 ;

x now equals 2 - cout << - - x ;

The computer will display the number 4 - cout << x - - ;

The computer will dispaly the number 5

( * ) Multiplication.

( / ) Division.

( % ) Division's remainder.

- x = x + 3 ;
__Logic__

( > ) Greater than.

( < ) Less than.

( > = ) Greater than or equal.

( < = ) Less than or equal.

( = = ) Equal. (Two adjacent equal's for logic operations.)

( ! = ) Not Equal.

( & & ) And. (Two adjacent and's for logic operations.)

( | | ) Or. (Two adjacent or's for logic operations.)

( ! ) Not.

__Statements and loops____If statemant__- if ( condition ) {

...... ;

...... ;

} - if ( condition ) {

...... ;

...... ;

}

else {

...... ;

...... ;

- if ( condition ) {
__While statement__- while ( condition ) {

...... ;

...... ;

} - do {

...... ;

...... ;

}while( condition )

- while ( condition ) {
__For loop__- for ( i=5 ; i>=2 ; i-- ) {

...... ;

...... ;

}

- for ( i=5 ; i>=2 ; i-- ) {
__Notes__- Break statement

It causes the programme to exit from the loop. - Continue statement

It causes ther programme to skip other loop statements and go to the start of the loop . - Return ( x )

It causes the function to terminate and returns the value of x .

- Break statement

__Functions__- Simple Function :

void fun1(void);

void main(void){

cout<<"\nMain programme";

fun1( );

getch( );

}

void fun1(void){

cout<<"\nFunction";

} - Passing and returning values :

int fun2(int x);

void main(void){

int y;

y=fun2(33);

cout<<y;

getch( );

}

int fun2(int x){

if(!(x%11)) {

cout<<"\nDivisible by eleven";

return (x/11);

}

else {

cout<<"\nNot divisible by eleven";

return 0;

}

} - Passing variables by references :

void fun3(int &x , int &y);

void main(void){

fun3(3,4);

cout<<x<<y;

getch( );

}

void fun3(int &x , int &y){

x++;

y++;

}

- Simple Function :

Copyright (C) 2000 Tarek Amr Abdullah. All rights reserved. My Home Page