Вы находитесь на странице: 1из 23

Basics of C++

Statements

Selection : if,switch

Iteration : while,for,do-while

Jump : break,continue,goto,return

Label: case,default

Expression

Block

Selection Statements
If
if(expression) statement;
else
statement;

Nested if
if(i)
{
if(j) statement 1;
if(k) statement 2;
else
statement 3;
}
else
statement 4;

Selection Statements
if else -if ladder

? Alternative

if(expression) statement 1;
else
if(expression) statemenet 2;
.
.
else
Statement;

if(condition)
expression;
else
expression;

Exp1?Exp2:Exp3

switch
switch(expression)
{
case constantt1:
statement sequence;
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence;
break;
.
.
default
statement sequence
}

Nested switch statements


switch(x)
{
case 1:
switch(y)
{
case 0:
cout<<First case;
break;
case 1:
funcCall(x,y);
}
break;
case 2:
.
.
}

Iteration Statements

for

for(initialization;condition;increment)

statement;
while

while(condition) statement;

do-while

do{

statement;
} while(condition);

Jump Statements

Return return expression;

Goto

goto label;
.

.
label:

break

continue

Arrays

Array a collection of variables of same type


Single dimension array
type var_name[size];
double dimension [100];
dimension[2]=30;
Pointer to Array
int sample[10];
int *p;
p=sample;

Arrays
Passing single dimension array to functions
A pointer to array is passed to function by specifying array's name
without an index.
int main()
{
int i[10];
func1(i);
.
.
}

Function receiving a single dimensional array


void func1(int *x) //Pointer
void func1(int x[10]) //Sized array
void func1(int x[ ]) //unsized array

An array name without index is a pointer to the first element in the array.
char p[5]; p,&p[0] are identical

Arrays
int *p,i[10];
p=i;
p[5]=100; //assign using index
*(p+5)=100; //assign using pointer arithmetic
//Accessing array by pointer arithmetic is faster than access
by array index.
int a[10[10];
a[j][k] is equivalent to *((base-type *)a+( j * rowlength)+k
To acess 0,4 element
array indexing a[0][4] Pointer *((int *)a+4
Cast of the pointer to array into a pointer of its base type is necessary
for the pointer arithmetic to operate properly

Pointers

Pointer is a variable that holds memory address.


type *name;
& returns the memory address of the operand.
* returns the value located at the address that follows

Indexing Pointers

char p[10];

int *p,i[10];

p=i;
p[5]=100;

*(p+5)=100;

Functions
ret-type function-name(parameter list)
{
//body of function
}

void show(); //Function declaration


main()
{
........
show(); //Function call
.......
}

void show() //Function definition


{
.... //Function body
}

A function's code is private to that function and cannot be accessed by any


statement in any other function except through a call to that function.

Functions
Call by Value :
Copies the value of an argument into formal parameter of the subroutine.
Changes made to the parameter have no effect on the argument.
Call by Reference:
Address of the argument passed to the function
Code within the function can change the value of the argument inside the
function.
Calling Functions with Arrays
When array is used as a function argument,its address is passed
to a function.

Structures

A structure is a collection of variables referenced


under one name, providing a convenient means of
keeping related information together.
struct address
{
char name[30];
char street[40];
char city[20];
char state[3];
unsigned long int zip;
};

struct struct-type-name
{
type member-name;
type member-name;
}structure-variables;

struct address addr_info;

address addr_info;

In C

In C++

Structures-cont'd

Accessing structure members

addr_info.zip=560100;
gets(addr_info.name);

Structure asignments
struct
{
int a;
int b;
}x,y;

x.a=10;
x.b=20;
y=x;//assign one structure to another
cout<<y.b;

Structure-cont'd

Arrays of structures

struct address addr_info[50];

To access a specific structure,index the structure


name.

To print the zipcode of structure 3

cout<<addr_info[2].zip;

Passing structures to function

Passing structure members to function


struct employee
{
char name[50];
int age;
float salary;
}emp1;

computePF(emp1.salary);

Passing entire structure members to function


struct struct_type
{int a,b;
char ch;
}arg;

f1(arg);
void f1(struct struct_type parm)
{

Structure Pointer
struct bal
{
float balance;
char name[30];
}person;

struct bal *p;


//declare a structure pointer
p=&person;
//places the address of the structure person
Into the pointer p.

To access the members of the a structure using a pointer


to that structure use -> operator
p->balance

Structures
struct x
{
int a[10][10];
float b;
}y;

struct emp
{
struct addr address;
float wage;
}worker;

To reference integer 3,7 in


Structure y
y.a[3][7]

worker.address.zip=93456;

Unions

Union is a memory location shared by two or more


different types of variables.
union union-type-name
{
type member_name;
.
.
}union_variable;

u_type convt;

Byte 0
ch

union u_type
{
int i;
char ch;
};

Byte 1

Enumeration

An enumerated type declares optional type name and


a set of zero or more identifiers that can be used as
values of the type.

enum enum-name {enumeration list}variable_list;

enum color{red,green=3,blue}

typedef

To define new data type name.

typedef type newname;

typedef float balance;

Informs the compiler to recognize balance as another


name for float.

balance overdue;(overdue is a floating point variable


of type balance)

Вам также может понравиться