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.
For Example:
#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.
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.
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
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.
Statements:
- Looping Statement
- Nesting of control structure
- Goto Statement
Looping:
- In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied.
- A program loop consist of two segments
- Body of the loop
- Control statement
- Two types of control structure:
- Entry-controlled loop
- Exit-controlled loop
While loop:
- Entry-controlled loop
- Syntax :
{
body of the loop
}
next statement;
Example:
void main()
{
int i = 0;
while (i<5)
{
printf(" the value of i is %d\n", i);
i=i+1 ;
}
}
Output :
the value of i is 0
the value of i is 1
the value of i is 2
the value of i is 3
the value of i is 4
- Program to calculate factorial of a given number using while loop.
void main ( )
{
int n, fact =1;
clrscr( ) ;
printf( “\n Enter the Number:”);
scanf(“%d”, &n);
while(n>=1)
{
fact = fact*n; n - - ;
}
printf(“ \n factorial of given number is %d”, fact);
getch( );
}
Do-While Loop:
- It tests the condition at the bottom of the loop after executing the body of the loop. we can be assured that the body of the loop is executed at least once.
- It is mainly used in menu like programs where all the available choices are printed at least once.
- Syntax :
do
{
statement;
}
while(condition);
Flow chart:
Example:
void main()
{
int i,n = 5;
i = 0;
do
{
printf("the numbers are %d \n",i);
i=i +1;
}
while( i<n)
}
Output :
the numbers are 0
the numbers are 1
the numbers are 2
the numbers are 3
the numbers are 4
Program to calculate factorial of a given number using do while loop:
void main ( )
{
int n, fact =1;
clrscr( ) ;
printf( “\n Enter the Number:”);
scanf(“%d”, &n);
do
{
fact = fact * n;
n- -;
}
while
(n >= 1);
printf(“ \n factorial of given number is %d”, fact);
getch( );
}
For Loop:
- Entry-controlled loop
- Used when the number of iterations is predetermined.
- Syntax :
for (initialization;test condition; increment/decrement)
{
block of code
}
next statement;
Flow chart:
Example:
void main()
{
int i, n=5;
for (i=0;i<n; i=i+1)
printf (“The numbers are %d \n",i);
}
Output :
The numbers are 0
The numbers are 1
The numbers are 2
The numbers are 3
The numbers are 4
Nested For Loop:
- If a for loop is defined in another for loop, it is called nested for loop.
- The outer loop is executed first of all.
- The general format of nested for loop is:
{
for (initialization; test condition; increment/decrement)
{
block of code
}
block of code
}
next statement;
*
* *
* * *
* * * *
* * * * *
void main( )
{
int x, i, j ;
printf(“How many lines stars (*) should be printed? :”);
scanf(“%d”, &x);
for(i=1; i<=x; i++)
{
for (j=1; j < =i; j++)
{
printf( “*”);
}
printf( “ \n”);
}
getch( );
}
Continue Statement:
- The continue statement is used to bypass the remainder of the current pass through a loop.
- The loop does not terminate when a continue statement is encountered, instead, the remaining loop statements are skipped and the computation proceeds directly to the next pass through the loop.
- The continue statement can be included within a while, a do-while, a for statement.
- It is simply written as “continue”.
- The continue statement tells the compiler “Skip the following Statements and continue with the next Iteration”.
- In while and do loops continue causes the control to go directly to the test – condition and then to continue the iteration process.
- In the case of for loop, the updation section of the loop is executed before test-condition, is evaluated.
Go to Statement:
- The goto statement is a jump statement which jumps from one point to another point within a function.
- Used to transfer the program control unconditionally from one statement to another statement.
- Syntax :
- goto label;
…………
label: statement;
or
label: statement;
.......
.......
goto label;
Example:
void main()
{
int n = 0;
Loop:
printf ("\n%d", n);
n++;
if (n<10)
{
goto Loop;
}
getch();
}
1 Comments
Casino Tycoon Hotel, Atlantic City, NJ 08401 - MapYRO
ReplyDeleteGet directions, reviews and 원주 출장안마 information for 양산 출장샵 Casino Tycoon Hotel, Atlantic 천안 출장마사지 City, 천안 출장샵 NJ 08401 in Atlantic City, NJ. 전라북도 출장마사지