Pseudocode
Pseudocode
Pseudocode (derived from pseudo and code) is a compact and informal high-
level description of a computer programming algorithm that uses the structural
conventions of programming languages, but omits detailed subroutines, variable
declarations or language-specific syntax. The programming language is improved with
natural language descriptions of the details and with compact mathematical notation.
The purpose of using pseudo code as compare the programming language syntax is
that it is easier for humans to read. Pseudocode may therefore vary widely in style,
from a near-exact imitation of a real programming language or may be differ from real
programming language syntax. Flowcharts can be thought of as a graphical form of
pseudo code.
Examples of pseudocode –
Algorithm
An algorithm is a formula or set of steps for solving a particular problem. An
algorithm is a set of rules must be unambiguous and have a clear stopping point.
Algorithms can be expressed in any natural languages like Hindi, English, French etc.
The concept of algorithm is also used to define the notion of logic. That notion
is central for explaining how formal systems come into being starting from a small set
of axioms and rules. In logic, Different algorithms may complete the same task with a
different set of instructions in less or more time, space, or effort than others.
Cooking recipe is also algorithm that explains systematic process & steps for Food
preparation.
Flowchart
The flowchart is a means of visually presenting the flow of data through an
information processing systems, the operations performed within the system and the
sequence in which they are performed.
Magnetic Tape
Magnetic Disk
Off-page connector
Flow line
Annotation
Display
In drawing a proper flowchart, all necessary requirements should be listed out in
logical order.
The flowchart should be clear, neat and easy to follow. There should not be any
room for ambiguity in understanding the flowchart.
The usual direction of the flow of a procedure or system is from left to right or top
to bottom.
Within each symbol, write down what the symbol represents. This could be the
start or finish of the process, the action to be taken, or the decision to be made.
Only one flow line should come out from a process symbol.
Only one flow line should enter a decision symbol, but two or three flow lines,
one for each possible answer, should leave the decision symbol.
Only one flow line is used in conjunction with terminal symbol.
Decision Table
A Decision Table is a matrix of rows and columns that shows condition & actions.
Decision Table and Decision Tree are use to express relationship between Condition
and related actions. These tools are use during System Development Life Cycle
[SDLC].
1. Condition Statements
2. Condition Entries
3. Action Statements
4. Action Entries
Condition Statements
Condition Statements identifies the relevant condition.
Condition Entries
Condition Entries tells what a value to be applies for a particular condition.
Action Statements
Action Statements list the set of all steps\actions that can be taken when a certain
condition occurs.
Action Entries
Action Entries shows what specific action/ step in the set to take when selected
condition is true.
Sometime it is also mention with table when to use this table or to distinguish this
table from other tables.
ConditionDecision Rules
Example -
ConditionDecision Rules
In above example decision table have four parts i.e. Condition Statements,
Action Statements, Condition Entries and Action Entries. Action taken is depends on
condition statements and condition entries, if condition statement is yes [Y] then
associated action statement executes denoted by X sign.
Programming Language
A formal language in which computer programs are written. The definition of a
particular language consists of both syntax (how the various symbols of the language
may be combined) and semantics (the meaning of the language constructs).
The language a programmer uses to create an application will depend on the desired
properties of the program. Some programming languages lend themselves to
mathematical and analytical functions while others are better suited for creating
business or data processing applications.
Data Type
A data type defines a set of values that a variable can store along with a set of
operation that can be performed on that variable. A variable is named location in main
memory that is used for holding a value that may be modify by the program.
The size & range of these data type may vary between processor. The range of
float & double is also depend on method used to represent it.
Different data types are required because program requires to read (Input) &
write (output) different type of values.
int (integer) %i or %d
It is used to accept Positive & Negative Numeric values.
Integer variable does not accept decimal part of value.
Generally Range of int. is from -32768 to + 32767.
Format Specified for int. is %i or %d.
int k, m = 17;
unsigned int %u
It is used to accept Positive Numeric values. Integer variable does not accept
decimal part of value. Generally Range of unsigned int. is from 0 to 65,535
Format specifier for unsigned int is %u
unsigned int k, m = 50000;
Declare two variables k, m of datatype unsigned int and assigning value 50000 to
variable m.
float %f or %g or %e
It also accepts positive & Negative Real type Numeric values.
Float type variable contains value with decimal point.
Format specifier for float is % f.
double %lf
double type variable accept Real type Numeric value.
This contain value with decimal point.
Format specifier for double is %lf
Identifiers
Name of variable, constant, function, Labels and various other user define
objects are called identifier.
Following are rules for constructing Identifiers :
(i) Identifier Name must start with alpha bate or underscore sign.
(ii) Identifier Name may contain alpha bate {a to z or A to Z}
Number {0 to 9} and under score sign only.
(iii) Special symbol {Like #, $ etc.} are not allowed in identifer name.
(iv) Maximum 8 character are allowed in identifier name.
(v) Two or more identifier should not have same name.
(vi) Blank space is not allowed within identifier name.
Example
Example of some valid Identifier (Names)-
Count
Abc
xy212
Add11
Sum_13
Example of some Invalid Identifier (Names)-
1New
NM 14
Total $ 43
Variables
A variable is named location in memory that is used for hold a value that may be
modify by program. Variable must be declare before any executable statement.
A data type associate with variable define the set of values that may be store by
that variable and set of operation possible on that variable.
int a, b, c;
float x, z = 10.75;
Types of Variables –
Constant
Like a variable, a constant is named location in memory that is used to hold
value.
But value of a constant can not be change by program at Run Time.
Constants are also called literals.
For declaration of constant we should use const keyword with datatype and
constant identifier name.
Const int. k = 15;
Const float n = 12.67;
Constant can be divided into two categories-
(i) Primary Constant
(ii) Secondary constant
Const keyword is access modifier that stop to modify value of memory location.
Like variable, constant may be declare at three locations.
Reserve Words
Reserve words are also called keywords. Reserve words are identifiers whose
meaning has already been define C compiler. Reserve words can not be used as
variable or constant name because assigning a new meaning to the reserve word is
not possible.
Reserve words are identifiers whose meaning has already been define C compiler.
So we do not need any header file to use for using keywords in our C program.
Some complier may have some additional keywords also. Following are some
examples of keywords / reserve words available in C programming language –
1) auto 2) static 3) extern 4) register 5) int 6) long 7) float 8) double 9) char 10) void
11) signed 12) do 13) for 14) while 15) if 16) else 17) switch 18) case 19) break
20) continue 21) default 22) goto 23) struct 24) Union 25) return
Operators
Binary operators
Arithmetic Operators
% Modulus operator
/ Division
* Multiplication
+ Addition
- Subtraction [Also known as Unary Minus]
Truth Tables –
Truth Table for && [Logical And]
Condition A Condition B Result
True True True
True False False
False True False
False False False
int a=10, b=24;
( ( a < 15 ) && ( b == 24 ) ) /* True */
Because a < 15 is true and b == 24 true i.e. True && True is True
( ( a != 15 ) || ( b != 24 ) ) /* True */
Because a != 15 is true or b != 24 false i.e. True || False is True
( ! ( k > 20 ) ) /* True */
Because k > 20 is false but ( ! ( False ) ) is True.
Relational & Logical Operator always produce value in terms of either Non zero [True]
or zero [false].
int a = 10, b = 20, c = 10;
b>a;
return true because b is greater
then a i.e. 20 > 10,
a < b;
Return True because value of a is Less then value of b.
a = = c;
Return true because value of a is equal to value of C.
a = = b;
Return false because value of a is Not equal to b.
a!=b;
Return True, because value of a is not equal to value of b.
a ! = c ; False
c > = b; false
b > = a; True
b > = c; True.
Bitwise Operator
Bitwise Operator
Operator Meaning
& And
¦ OR
^ Exclusive OR [XOR]
~ Not
>> Right Shift
<< Left Shift.
The bitwise And, or and Not are governed by the truth table.
The Right shift (>>) and left shift (<<) move bit of variable in Right or Left as
specified.
Variable >> No. of bits shifting
variable << No. of bits shifting.
By using right or left shift, bits are shifted off from one end & O [zero] are brought
from other end.
Assignment Operator
Assignment Operator is Binary Operator. Assignment Operator can be used with
any valid expression. Left side part of assignment operator must be variable or pointer
name.
Constant or function name of left side of assignment operator is not allowed.
Right side of assignment operator may be constant, function, value, variable or
expression.
int k;
k = a + b;
k = 17;
Two terms LValue & RValue are used with assignment operator.
LValue means any object that can be used on left side of assignment operator.
RValue means any object that can be used on right side of assignment operator.
int a, b, c;
a = b = c = 75;
Above is example of multiple assignment that means single value can be assign
to multiple variable within single statement. Assignment operator works from right to
left order.
Unary operators
Increment ( + + ) and Decrement ( – – )
Both Increment & Decrement operators are Unary operators. Increment (++)
operator adds 1 to its operand and Decrement (–) subtracts one from operand.
Unary operator Increment & Decrement have higher precedence then any binary
operator. Increment and Decrement operator may be :
Pre-Fix
OR
Post-Fix
The difference between prefix & postfix operator arise when these are used in
expression. Prefix operators are evaluated before expression evaluation, and Postfix
operator are evaluated after expression evaluation.
int k;
k ++ ; /* Postfix Increment */
++ k ; / * Prefix Increment */
– – k; / * Prefix Decrement */
k – – ; /* Postfix Decrement */
Jumping Statement
Jumping Statements are used to transfer execution control to any other point
within the program during execution time. After transferring execution control
program execution flow, resume sequential from that point. In C Programming
followings are jumping statement –
break
continue
goto
return
break Statement
Break Statement can be used within any loop statement or with Switch-Case
statement.
Keyword break forces to immediate termination of a loop or switch-case
statement. Normal loop/switch termination condition is by passed when break is
used.
Break statement sends execution control outside of loop/switch statement &
resume sequential execution flow after loop/switch-case part.
int k;
For (k = O; k < 75; k++)
{
printf (``\n value = %d'', k);
if (k = = 15)
break;
Above loop is executed & printed till value of k is 15 and after that if ( ) condition
is true & break executed that causes immediate termination of loop & bypass the
normal termination condition of loop.
continue Statement
Continue is a keyword in C language. Continue statement can be used with :
(i) for loop
(ii) while loop
(iii) do-while loop
When continue statement execute; remaining part of loop is skipped & execution
control pass to starting of loop for next iteration of loop.
void main ( )
{
long k, m = 1;
clrscr ( );
printf (''Program for multiplication '').
printf (''\n for exit press O '');
for (; ;)
{
printf (``\n Enter any positive integer value '');
scanf (``%/d ''value & k);
if (k = = 1)
Continue;
if (k = = O)
break ;
m = m * k;
3 /* end of for loop */
printf (''/n Multiplication = %/d'', m)
getch ( ) ;
} /* end of void main () */
goto Statement
void main ( )
{
int k = O;
clrscr ( );
indore : /* label indore */
k++ ;
printf (''\n %d = Indore'', k);
if (k = = 10)
exit (0); /* terminate Prog. execution */
goto indore ; /* jump to label indore */
getch ( );
} /*end of join main () */
return Statement
The keyword return is used to return execution control from a function. Return
statement can be categorized as a jump statement because it returns [Jump back]
from a function to point of function calling.
The return statement can be used within Non-void function. In other words
return statement can be used within a function if function data type is not void.
Void function can not be used with return statement. In a non-void function if
return is not used, function returns garbage value.
The return statement can be used anywhere within a non-void function and as
soon as return statement function execution stop & execution control returns to
function calling point.
general form of return statement-
return (value/variable/constant/expression );
exit function
exit ( ) is not a kind of jump statement. On execution of exit ( ) statement entire
program execution terminates immediately.
exit ( ), statement returns execution control from program to operating system.
The exit ( ) is library function of C language.
General form of exit ( ) is-
void exit (int return value);
stdlib.h header file is required to includes for exit ( ) function. exit ( ) return O
[Zero] to operating system on normal terminator.
Conditional Statement
if ( ) Condition
if ( ), if ( )- else & Switch are conditional statement. This conditional statement is
also called selection statement. Conditional / Selection statements are used to control
execution flow of program.
Sometimes it is required for execution of a particular statement if certain
condition is true & otherwise another statement execution is required in such case
conditional statements are required.
It is called conditional / selection statement because it depend on condition that
what statement will execute next or selection of execution path is condition based.
Following conditional statements are available in C Language-
(i) if (Condition)
{
Statement 1;
Statement 2;
-------------
-------------
Statement N;
}
(ii) if (Condition)
{
Statement 1;
Statement 2;
-------------
-------------
Statement N;
}
else
{
statement 1;
Statement 2;
-------------
-------------
Statement N;
}
case condition 2;
Statement Block;
break;
case Condition N :
Statement Block;
break ;
default ;
statement Block;
} /* end of switch */
if (k < 10)
{
printf (``\n k is greater then 10'')
printf (``\n value of k = %d'', k);
}
Value k is 15 i.e. k > 10 so condition is true & both statement will execute.
Sequentially.
if ( Condition ) - else
if (Condition)
{
Statement 1;
Statement 2;
----------------
----------------
Statement N;
}
else
{
Statement 1;
Statement 2;
----------------
----------------
Statement N;
}
if ( ) else format of conditional statement is used when there are only two
conditions & one of them is true.
In other words we can say when one condition is true that ensure that another
one is false and first one is false that ensure that second is true then we use if () else
format of conditional statement.
Else is always used after if & with else condition is never specified. We can use if
without else but vice versa is not true.
if condition is true then statement related with if will executes and statement
related with else skips.
if condition is false then if statement will skip and Else statement will execute.
In short execution of else depend on if. If is true then else will not execute
otherwise else executes.
else part is optional if & Else is not used then it is a simple if conditional
statement.
Example-
if (a > = b)
{
printf (" Value of a = %d is greater ''; a);
} ( * end of if condition */)
else
{
printf (''\n value of b = %d is greater'', b);
}/* end of else part */
Here are only two conditions either value of a is greater or b. So if else format is
used. In this example condition specified with if is false, so if part is skiped and Else
part will execute.
When there are more then two conditions and only one is true then if- else if-else
format is used.
if (condition)
{
Statement block;
}
else if (condition)
{
Statement block;
}
else if (condition)
{
Statement block;
}
else
{
Statement block;
}
The conditions are evaluated from the top to down approach looking for first true
condition.
If all conditions in ladder are false then else part will execute.
else part is optional to use. If else part is not present and none condition is true
then no statement will execute.
int n = 25;
if (n > = 40)
{
printf (" Value of n = %d'', n);
}
else if (n > = 30)
{
printf (" Value of n = %d'', n);
}
else if (n > = 20)
{
printf (" Value of n = %d'', n);
}
else
{
printf (" Value of n = %d'', n);
}
Conditions are checked in Top to Down way & looking for true condition. This
Condition i.e. (n > = 20) is true so related statement will executes & others are
skipped.
With if (Condition), if (Condition)- else or if (Condition)- else if (Condition)- else
ladder, we can use any loop. Statement, Conditional Statement, Jumping Statement
or I/O Statement as a valid Statement.
Switch Statement
C Programming Language has a built in multiple branch selection statement
called switch statement.
Switch tests for equality condition in top to down approach. When condition is
satisfied associate statements are execute and remain part switch is skipped.
Switch only checks for equality and so no Relational operator be can used with
Switch-case statements.
For multiple conditions even we can use if-else if ( ) - else ladder but increasing
depth of if-else if ( )- else ladder makes it difficult to understand so in place of this we
can use switch statement.
According to standard C specification; switch can have at least 257 case
statement, but if may vary from complier to complier.
case constant 2;
Statement block 2;
break;
case constant 3;
Statement block 3;
break;
case constant 4;
statement block 4;
break;
default ;
statement block;
} /* end of switch statement */
The value of expression is tested against the value of the constant in the case
statement. When match is found the statement block associate with case will execute.
If none case statement will execute then default execution is performed. Use of
default is optional.
With case we can only use constant or value, i.e. No variable can be used with
case. Expression must be integer or character Floating point expression is not
allowed. No, two case constant in the same switch can have identical values.
Loop Structure
for Loop
int k;
for (k = 0; k ... = 10; k++)
{
printf (``\n%d-Exam '', k);
}
int z;
for (z = 5; z > 0; z –)
{
printf (``\n%d - DAVV '', z);
}
This loop is executed 5 times & each time loop executes, value of z is decrement
by 1. The for loop may be executed zero times if initial loop condition is false.
while (Condition)
{
Statement 1 ;
Statement 2;
Statement N;
}
While loop is executed till condition is true. When condition is false while loop
execution is terminated and execution control passes to instruction after code.
While loop checks the condition first, if condition is false then loop executes zero
times.
int k = 1;
This loop is executed 10 times & when value of k became 11 then condition (k <
= 10) is false and loop execution is terminated.
While loop have only 1 part for condition checking, so assignment of value to
control variable is done before loop execution starts.
Value of control variable is increased/decreased within the while loop body.
Do-While
In for & while loop, condition is checked in starting of loop. In do-while loop
always executes at least once because condition is checked in end of loop.
do
{
statement 1;
Statement 2;
Statement N;
}
while (condition);
int k = 5;
do
{
printf {``This statement executes only once'');
K++;
}
while (k > 10);
This loop is executed once even though condition is false because do-while
check condition in last of loop.
in z = 1;
do
{
printf (``\n %d = Exam '', z);
z ++;
}
while (z > = 10);
This loop execute 10 times till value of z is 10; i.e. condition is true.
Pointers
Pointer is variable that hold memory address of variables or any other object like
functions. Pointer is useful for three purposes-
1. For Call by reference [Function Calling] - In call by reference, function is
called by passing address of variable(s) instead of passing value(s) to function called.
Pointer(s) are used in formal parameter list of function called in case of call by reference.
Advantage of call by reference is that more than one function can share same memory
location(s).
For example -
int *p;
p = (int) malloc(sizeof (int)) ; /* Dynamic memory allocation */
A Pointer declaration consists of three parts i.e. Data Types, *sign and variable
name. For Example-
int *a, *b;
a & b are pointers that hold address of variable of int type.
int n, m;
a = & n; /* a pointer hold address of n variable */
b = &m; /* b pointer hold address of m variable */
float *x, *z;
float j,k;
x & z are pointers that hold address of float type variable.
x = &j; /* pointer hold address of j variable */
z = &k;/*z pointer hold address of k variable */
Operator & means "address of''
With pointers only addition (+, ++) and subtraction (-, –)
operations are allowed. For example-
x++;
- –b;
void main( )
{
int a, b;
long c;
clrscr ();
getch ();
} /* end of void main () */
ARRAY
An array is a collection of variables of the same type and they are referred
through a common name. A specific array element is accessed by using array name
and Index Value of that particular element.
Index value of array elements always starts from zero, i.e. Index value of first
array element is zero. All array elements are located at continuous memory location.
In C language, array element number and boundary checking is responsibility of
program.
The lowest address corresponds to the first array element and the highest
address of the last array element.
Array may be -
(i) One Dimension Array [1-D Array]
(ii) Two Dimension Array [2-D Array]
(iii) Multi Dimension Array [N-D Array]
(ii) Two Dimension Array- [2-D Array or Two-D Array is simplest form of multi-
dimension Array.
Two Dimension array are stored in row-column matrix format. The generalized
form of 2-D array is -
datatype array name [m] [n] ; /*ArrayName [row][column]*/
Where m is number of Row &
n is number of Column
long k[5] [3] ;
Identifier k is name of 2-D array, having 05 rows & 03 columns. Array k have
total 5 x 3 = 15 elements. Array indexing in 2-D array is also start from 0,0 [zero, zero]
location that is very first element of 2-D array.
(iii) Multi-Dimension Array [N-D Array] - More then 2-D array is known as
Multi Dimension Array. it may be 3-D, 4-D, 5-D or so on.
Normally more then 3-D array is not used because that requires more time to
calculate element location in main memory and so required more time to access
element values.
The generalize form of N-D array is- Data-type array name [Siz 1] [Size 2] [Size
3] [Size 4] ........ [Size N] ;
Array indexing always start from 0 index value.
Advantages of array