Control Structure with example of flowchart and program:

Definition:

  •    <Book Content> The logic of a program may not always be a linear sequence of statements to be executed in that order.
  •    The  logic of the program may require execution of a statement  based on a decision.
  • Control structures specify the  statements to be executed and the order of execution of statements <Book Content>

Where is the usage of control structure:

Flowchart and Pseudo Code use Control structures for representation

There are three kind of Control Structure:

1). Sequential  

-<Book Content>Instruction are executed in linear order<Book Content>

-Statements are executed in a specified order. No statement is skipped and no statement is executed more than once

For Example Code:
#include<stdio.h>
void main()
int a;
{
int a=5;
printf(“Square of a = %d”,a);
}

                                                            Example of Flow Chart:

5380975_orig

2). Selection(Branch or conditional )

 -<Book Content> it’s ask a true/false question THEN select the  next instruction  based on the answer<Book Content>

 -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.

For Example Code:
#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();
}

 

2603562_orig

3). Iterative(loop)

 -<Book Content>It’s repeat the execution of a block of instruction<Book Content>

-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.

For Example Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=1;
clrscr();
while(a<=5)
{
printf(“I Love India\n”);
a++;
} //end of while
} //end of main

For Example Flow Chart:

1724987_orig

 

 

 

 

Leave a comment