Академический Документы
Профессиональный Документы
Культура Документы
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
}
Iteration Statements
for
for(initialization;condition;increment)
statement;
while
while(condition) statement;
do-while
do{
statement;
} while(condition);
Jump Statements
Goto
goto label;
.
.
label:
break
continue
Arrays
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);
.
.
}
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
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
}
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
struct struct-type-name
{
type member-name;
type member-name;
}structure-variables;
address addr_info;
In C
In C++
Structures-cont'd
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
cout<<addr_info[2].zip;
computePF(emp1.salary);
f1(arg);
void f1(struct struct_type parm)
{
Structure Pointer
struct bal
{
float balance;
char name[30];
}person;
Structures
struct x
{
int a[10][10];
float b;
}y;
struct emp
{
struct addr address;
float wage;
}worker;
worker.address.zip=93456;
Unions
u_type convt;
Byte 0
ch
union u_type
{
int i;
char ch;
};
Byte 1
Enumeration
enum color{red,green=3,blue}
typedef