Professional Documents
Culture Documents
com
1. Name
2. Type
3. Size
4. Value
Data Types
1. int
2. short
3. long
4. float
5. double
6. char
Arithmetic operators
Plus +
Minus -
Multiply *
Divide /
Modulus %
Precedence
Highest: ()
Next: *,/,%
Lowest: +,-
Key Words of C
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
main
if
else
while
do
for
Statements in C
If (condition)
statement ;
If ( condition )
statement1;
statement2;
Example:
Relational Operators:
< less than
== equal to
!= not equal to
Logical Operators:
AND &&
OR ||
In C
Example:
if-else
if (condition)
statement ;
else
statement ;
}
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Code
if AmirAge > AmaraAge)
else
Nested if
If (age > 18)
If(height > 5)
}
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
int sum ;
sum = 0 ;
sum = 0 ;
number = 1 ;
number = number + 1 ;
}
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
cout << “ The sum of the first 1000 integer starting from 1 is ” << sum;
or more times.
What if we
do-while
Do while loop execute on
or more times
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
do
statements ;
while ( condition )
for Loop
statement ( s ) ;
Example:
int counter ;
Output
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
0123456789
Increment operator:
++
Decrement operator
--
Multi-way decision:
If statement, If else, and finally switch statement
case ‘c’ :
statements;
…
}
Example:
switch ( grade)
{
case ‘A’ :
cout << “ Excellent ” ;
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
case ‘B’ :
cout << “ Very Good ” ;
case ‘C’ :
case ‘D’ :
cout << “ Poor ” ;
case ‘F’ :
cout << “ Fail ” ;
break;
if (c == ‘z’ )
continue
….
….
continue ;
Decisions
– if , if else , switch
Loops
Structured Programming
Sequences
Decisions
Loops
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Function
Function name
Declaration of Function
main ( )
:
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Definition of Function
Definition
return ( i * i ) ;
Function Call
int x ;
x = square ( i ) ;
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Arrays
They are special kind of data type
– name
– data type
– size
memory
Declaration of Arrays
arrayType arrayName[numberOfElements ];
For example ,
int age [ 10 ] ;
Initializing an Array
int age [ 10 ] ;
for ( i = 0 ; i < 10 ; i ++ )
{
age [ i ] = 0 ;
}
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Copying Arrays
for ( i =0 ; i < 10 ; i ++ )
b[i]=a[i];
Initializing an Array
int c [ 10 ] = { 1,2,3,4,5,6,7,8,9,10 } ;
int c [ ] = { 1,2,3,4,5,6,7,8,9,10 } ;
Pointers
Declaring Pointer to Integer
int *myptr ;
int *ptr , x ;
int *ptr , x , a [ 10 ] ;
const
1. int *const myptr = &x ;
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
int *yptr ;
yptr = y ;
Pointer Arithmetic
*yptr + 3 ;
Pointer Comparison
Strings
String Initialization
char name [ 20 ] ;
name [ 0 ] = ‘A’ ;
name [ 1 ] = ‘m’ ;
name [ 2 ] = ‘i’ ;
name [ 3 ] = ‘r’ ;
name [ 4 ] = ‘\0’ ;
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Array of Pointers
char *myArray [ 10 ] ;
Initialization
char *myArray [ ] = { “Amir ” , “ Jahangir ” } ;
0331=6788362
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
0331=6788362