Tuesday, 7 May 2013
Structured Programming Approach
Structured Programming Approach: The basic principal of structured programming approach is to divide a program in functions and modules. The use of modules and functions makes the program more comprehensible (understandable). It helps to write cleaner code and helps to maintain control over each function. This approach gives importance to functions rather than data. It focuses on the development of large software applications. The programming languages: PASCAL and C follow this approach.
Procedural Programming Approach
Procedural Programming Approach: This approach is top down approach. In this approach, a program is divided into functions that perform a specific task. Data is global and all the functions can access the global data. Program flow control is achieved through function calls and goto statements. This approach avoids repetition of code which is the main drawback of Monolithic Approach. The basic drawback of Procedural Programming Approach is that data is not secured because data is global and can be accessed by any function. This approach is mainly used for medium sized applications. The programming languages: FORTRAN and COBOL follow this approach.
Monolithic Programming Approach:
Monolithic Programming Approach: In this approach, the program consists of sequence of statements that modify data. All the statements of the program are Global throughout the whole program. The program control is achieved through the use of jumps i.e. goto statements. In this approach, code is duplicated each time because there is no support for the function. Data is not fully protected as it can be accessed from any portion of the program. So this approach is useful for designing small and simple programs. The programming languages like ASSEMBLY and BASIC follow this approach.
Jumping Statements:
Jumping Statements:
Break
Statement :
Sometimes, it is necessary to exit immediately from a loop as
soon as the condition is satisfied.
When break statement is used inside a loop, then it can cause to
terminate from a loop. The statements after break statement are skipped.
Syntax :
break;
Figure :
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1; ; i++)
{
if(i>5)
break;
printf("%d",i); // 5 times only
}
getch();
}
Output :
12345_
Continue Statement :
Sometimes,
it is required to skip a part of a body of loop under specific conditions. So,
C supports 'continue' statement to overcome this anomaly.
The
working structure of 'continue' is similar as that of that break statement but
difference is that it cannot terminate the loop. It causes the loop to be
continued with next iteration after skipping statements in between. Continue
statement simply skipps statements and continues next iteration.
Syntax :
continue;
Figure :
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10; i++)
{
if(i==6)
continue;
printf("\n\t %d",i); // 6 is omitted
}
getch();
}
Output :
1
2
3
4
5
7
8
9
10_
Goto Statement :
It is
a well known as 'jumping statement.' It is primarily used to transfer the
control of execution to any place in a program. It is useful to provide
branching within a loop.
When
the loops are deeply nested at that if an error occurs then it is difficult to
get exited from such loops. Simple break statement cannot work here properly.
In this situations, goto statement is used.
Syntax :
goto [expr];
Figure :
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1, j;
clrscr();
while(i<=3)
{
for(j=1; j<=3; j++)
{
printf(" * ");
if(j==2)
goto stop;
}
i = i + 1;
}
stop:
printf("\n\n Exited !");
getch();
}
Output :
* *
Exited_
Looping Statements / Iterative Statements :
Looping
Statements / Iterative Statements :
'A loop' is a part of code of a program which is executed
repeatedly.
A loop is used using condition. The repetition is done until
condition becomes condition true.
A loop declaration and execution can be done in following ways.
o Check
condition to start a loop
o Initialize
loop with declaring a variable.
o Executing
statements inside loop.
o Increment
or decrement of value of a variable.
*
TYPES OF LOOPING STATEMENTS :
Basically, the types of looping statements depends on the
condition checking mode. Condition checking can be made in two ways as : Before
loop and after loop. So, there are 2(two) types of looping statements.
·
Entry controlled loop
·
Exit controlled loop
1. Entry controlled loop :
In such type of loop, the test condition is checked first before
the loop is executed.
Some common examples of this looping statements are :
2. Exit controlled loop :
In such type of loop, the loop is executed first. Then condition
is checked after block of statements are executed. The loop executed atleat one
time compulsarily.
Some common example of this looping statement is :
While loop :
This
is an entry controlled looping statement. It is used to repeat a block of
statements until condition becomes true.
Syntax:
while(condition)
{
statements;
increment/decrement;
}
In above syntax, the
condition is checked first. If it is true, then the program control flow goes
inside the loop and executes the block of statements associated with it. At the
end of loop increment or decrement is done to change in variable value. This
process continues until test condition satisfies.
Program :
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
a=1;
while(a<=5)
{
printf("\n Aasaan IT Classes");
a+=1 // i.e. a = a + 1
}
getch();
}
Output :
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
For loop :
This
is an entry controlled looping statement.
In
this loop structure, more than one variable can be initilized. One of the most
important feature of this loop is that the three actions can be taken at a time
like variable initilisation, condition checking and increment/decrement. The
for loop can be more concise and flexible than that of while and do-while
loops.
Syntax:
for(initialisation; test-condition; incre/decre)
{
statements;
}
In above syntax, the given three expressions are seperated by
';' (Semicolon)
Features :
o More
concise
o Easy to
use
o Highly
flexible
o More
than one variable can be initilized.
o More
than one increments can be applied.
o More
than two conditions can be used.
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
for(i=0; i<5; i++)
{
printf("\n\t Aasaan IT Classes"); // 5 times
}
getch();
}
Output :
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Do-While loop :
This
is an exit controlled looping statement.
Sometimes,
there is need to execute a block of statements first then to check condition.
At that time such type of a loop is used. In this, block of statements are
executed first and then condition is checked.
Syntax:
do
{
statements;
(increment/decrement);
}while(condition);
In
above syntax, the first the block of statements are executed. At the end of
loop, while statement is executed. If the resultant condition is true then
program control goes to evaluate the body of a loop once again. This process
continues till condition becomes true. When it becomes false, then the loop
terminates.
Note:
The while statement should be terminated with ; (semicolon).
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
a=1;
do
{
printf("\n\t Aasaan IT Classes"); // 5 times
a+=1; // i.e. a = a + 1
}while(a<=5);
a=6;
do
{
printf("\n\n\t Aasaan IT Classes"); // 1 time
a+=1; // i.e. a = a + 1
}while(a<=5);
getch();
}
Output :
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Aasaan IT Classes
Decision Making Statements / Conditional Statements :
Decision
Making Statements / Conditional Statements :
C program executes program sequentially. Sometimes, a program
requires checking of certain conditions in program execution. C provides
various key condition statements to check condition and execute statements
according conditional criteria.
These statements are called as 'Decision Making Statements' or
'Conditional Statements.'
Followings are the different conditional statements used in C.
If Statement :
This
is a conditional statement used in C to check condition or to control the flow
of execution of statements. This is also called as 'decision making statement
or control statement.' The execution of a whole program is done in one
direction only.
Syntax:
if(condition)
{
statements;
}
In above syntax, the
condition is checked first. If it is true, then the program control flow goes
inside the braces and executes the block of statements associated with it. If
it returns false, then program skips the braces. If there are more than 1 (one)
statements in if statement then use { } braces else it is not necessary to use.
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
a=5;
clrscr();
if(a>4)
printf("\nValue of A is greater than 4 !");
if(a==4)
printf("\n\n Value of A is 4 !");
getch();
}
Output :
Value of A is greater than 4 !_
f-Else Statement :
This
is also one of the most useful conditional statement used in C to check
conditions.
Syntax:
if(condition)
{
true statements;
}
else
{
false statements;
}
In above syntax, the
condition is checked first. If it is true, then the program control flow goes
inside the braces and executes the block of statements associated with it. If
it returns false, then it executes the else part of a program.
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
clrscr();
printf("\n Enter Number :");
scanf("%d",&no);
if(no%2==0)
printf("\n\n Number is even !");
else
printf("\n\n Number is odd !");
getch();
}
Output :
Enter Number : 11
Number is odd !_
Nested If-Else Statement :
It is
a conditional statement which is used when we want to check more than 1
conditions at a time in a same program. The conditions are executed from top to
bottom checking each condition whether it meets the conditional criteria or
not. If it found the condition is true then it executes the block of associated
statements of true part else it goes to next condition to execute.
Syntax:
if(condition)
{
if(condition)
{
statements;
}
else
{
statements;
}
}
else
{
statements;
}
In above syntax, the
condition is checked first. If it is true, then the program control flow goes
inside the braces and again checks the next condition. If it is true then it
executes the block of statements associated with it else executes else part.
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
clrscr();
printf("\n Enter Number :");
scanf("%d",&no);
if(no>0)
{
printf("\n\n Number is greater than 0 !");
}
else
{
if(no==0)
{
printf("\n\n It is 0 !");
}
else
{
printf("Number is less than 0 !");
}
}
getch();
}
Output :
Enter Number : 0
It is 0 !_
Switch case Statement :
This
is a multiple or multiway brancing decision making statement.
When
we use nested if-else statement to check more than 1 conditions then the
complexity of a program increases in case of a lot of conditions. Thus, the
program is difficult to read and maintain. So to overcome this problem, C
provides 'switch case'.
Switch
case checks the value of a expression against a case values, if condition
matches the case values then the control is transferred to that point.
Syntax:
switch(expression)
{
case expr1:
statements;
break;
case expr2:
statements;
break;
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
case exprn:
statements;
break;
default:
statements;
}
In
above syntax, switch, case, break are keywords.
expr1,
expr2 are known as 'case labels.'
Statements
inside case expression need not to be closed in braces.
Break
statement causes an exit from switch statement.
Default
case is optional case. When neither any match found, it executes.
Program
:
#include <stdio.h>
#include <conio.h>
void main()
{
int no;
clrscr();
printf("\n Enter any number from 1 to 3 :");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\n\n It is 1 !");
break;
case 2:
printf("\n\n It is 2 !");
break;
case 3:
printf("\n\n It is 3 !");
break;
default:
printf("\n\n Invalid number !");
}
getch();
}
Output 1
:
Enter any number from 1 to 3 : 3
It is 3 !_
Output 2
:
Enter any number from 1 to 3 : 5
Invalid number !_
* RULES FOR DECLARING SWITCH CASE :
·
The case label should be integer or character constant.
·
Each compound statement of a switch case should contain break
statement to exit from case.
·
Case labels must end with (:) colon.
* ADVANTAGES OF SWITCH CASE :
·
Easy to use.
·
Easy to find out errors.
·
Debugging is made easy in switch case.
·
Complexity of a program is minimized.
Subscribe to:
Posts (Atom)