Control Structure
A statement that is used to control the flow of execution in a program is called control structure. It combines instruction into logical unit. Logical unit has one entry point and one exit point.
Types of control structures
1. Sequence
2. Selection
3. Repetition
4. Function call
1. Sequence: Statements are executed in a specified order. No statement is skipped and no statement is executed more than once.
Flowchart
Types of control structures
1. Sequence
2. Selection
3. Repetition
4. Function call
1. Sequence: Statements are executed in a specified order. No statement is skipped and no statement is executed more than once.
Flowchart
For Example
#include<stdio.h>
void main()
int a;
{
int a=5;
printf(“Square of a = %d”,a);
}
#include<stdio.h>
void main()
int a;
{
int a=5;
printf(“Square of a = %d”,a);
}
2. Selection:
It selects a statement to execute on the basis of condition. Statement is executed when the condition is true and ignored when it is false e.g if, if else, switch structures.
Flowchart
Flowchart
For Example:
#include<stdio.h>
#include<conio.h>
void main ()
{
int y;
clrscr();
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year.",y);
else printf("%d is not a leap year.".y)
getch();
}
3. Repetition:
In this structure the statements are executed more than one time. It is also known as iteration or loop e.g while loop, for loop do-while loops etc.
Flow chart
Flow chart
For Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=5)
{
printf(“I Love Pakistan\n”);
a++;
} //end of while
} //end of main
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=5)
{
printf(“I Love Pakistan\n”);
a++;
} //end of while
} //end of main
4. Function call:
It is used to invite or call a piece of code or statement. In this case control jumps from main program to that piece of code and then returns back to main program.
Flow chart
Flow chart