CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.
com
In a program a variable has:
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
No expression on the left hand side of the assignment
Integer division truncates fractional part
Liberal use of brackets/parenthesis
Statements in C
If (condition)
statement ;
If ( condition )
statement1;
statement2;
Example:
if (age1 > age2)
cout<<“Student 1 is older than student 2” ;
Flow Chart for if statement:
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Relational Operators:
< less than
<= less than or equal to
== equal to
>= greater than or equal to
> greater than
!= not equal to
Logical Operators:
AND &&
OR ||
In C
Example:
if(a > b && c> d)
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
if(age > 18 || height > 5)
Flow Chart Symbols:
if-else
if (condition)
statement ;
else
statement ;
}
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Code
if AmirAge > AmaraAge)
cout<< “Amir is older than Amara” ;
else
cout<<“Amir is younger than Amara” ;
Nested if
If (age > 18)
If(height > 5)
}
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Loop - Repetition structure
1.While
while ( Logical Expression )
{
statements;
:
}
int sum ;
sum = 0 ;
while (number <= UpperLimit)
int sum , number ;
sum = 0 ;
number = 1 ;
while ( number <= 1000 )
sum = sum + number ;
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;
It executes zero or more times
While loop executes zero
or more times.
What if we
want the loop to execute
at least one time?
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
for ( initialization condition ; termination condition ; increment condition )
statement ( s ) ;
Example:
int counter ;
for( counter = 0 ; counter < 10 ; counter = counter + 1 )
cout << 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
switch ( variable name )
{
case ‘a’ :
statements;
case ‘b’ :
statements;
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’ :
cout << “Good ” ;
case ‘D’ :
cout << “ Poor ” ;
case ‘F’ :
cout << “ Fail ” ;
break;
if (c == ‘z’ )
cout << “ Great ! You have made the correct guess “ ;
break ;
continue
while trynum <= 5 ;
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
….
….
continue ;
Minimize the use of break
Minimize the use of continue
Never use goto
What have we done till now …
Sequential Statements
Decisions
– if , if else , switch
Loops
– while , do while , for
Structured Programming
Sequences
Decisions
Loops
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Function
Function name
Body of the function
Two types of functions:
1. Functions that return a value
2. Functions that do not return a value
return-value-type function-name( argument-list )
{
declarations and statements
}
Declaration of Function
return-value-type function-name( argument--type-list) ;
main ( )
:
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
Definition of Function
int function-name ( int i , double j )
Return Type of Function
Declaration
int square ( int ) ;
Definition
int square ( int i )
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
They are like data structures in which
identical data types are stored
In C each array has
– name
– data type
– size
They occupy continuous area of
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 } ;
For character arrays
char name [ 100 ] = { ‘a’,b’,’c’,’0’,’1’ } ;
char name [ 100 ] = “abc01“ ;
char name [ ] = “Hello World“ ;
Pointers
Declaring Pointer to Integer
int *myptr ;
int *ptr1 , *ptr2 , *ptr3 ;
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
myptr is a constant pointer to an integer
2. const int *myptr = &x ;
myptr is a pointer to a constant integer
Declaration of a Pointer Variable
int y [ 10 ] ;
int *yptr ;
yptr = y ;
Pointer Arithmetic
*yptr + 3 ;
Pointer Comparison
if ( *y1 > *y2 )
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
Strings is always terminated with \0
char myName [ ] = “Amir” ;
char *myNamePtr = “Amir” ;
Array of Pointers
char *myArray [ 10 ] ;
myArray is an array of 10 pointer to character
Initialization
char *myArray [ ] = { “Amir ” , “ Jahangir ” } ;
Command Line Arguments
‘argc’ stands for a count of the number of arguments
‘argv’ stands for a vector of arguments
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
0331=6788362
CS201 Notes for Midterm written by Rana Abubakar khan www.ranapk.com
0331=6788362