By P.Vinitha M.SC., M.Phil
By P.Vinitha M.SC., M.Phil
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\nGood Morning");
printf("\nGood Morning");
printf("\nGood Morning");
printf("\nGood Morning");
printf("\nGood Morning");
getch();
}
The same program can be written using the for loop
statement as
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=0;i<5;i++)
{
printf("\nGood Morning");
}
getch();
}
The output of the above two programs are the same but the
length of program is reduced much.
Looping statements are used to execute group of statements
repeatedly until a given condition is satisfied.
The looping statements consists of two part
The body of the loop – The statements to be repeatedly
execute.
The control statement – Condition to execute body of the
loop.
The looping statements available in C are
while loop
for loop
The while loop is used when a statement is to be
executed repeatedly until a given condition is satisfied.
Syntax:
while(condition)
{
Body of the loop
}
Next statement;
False
Test
Condition
True
Body of the Loop
Next Statement
1. Check the condition.
2. If the condition is true
Execute the body of the loop. Goto step1
else
Goto next statement
In case of while statement the condition is
checked before entering the body of the loop. So
this is called entry-controlled loop.
The body of the loop may not be executed at
least once if the condition becomes false at the
first attempt.
The loop variable which is the variable used in the test
statement.
condition.
/* Example for while statement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=5)
{
printf("\nGood Morning");
i++;
}
getch();
}
/* Program to find the sum of digits of a number */
#include<stdio.h>
void main()
{
int n,m,rem,sum=0;
printf("Enter the number:");
scanf("%d",&n);
m=n;
while(m!=0)
{
rem=m%10;
sum=sum+rem;
m=m/10;
}
printf("The sum of the digits of the number %d is %d",n,sum);
}
DO..WHILE
STATEMENT
Do..while loop is used when it is necessary to execute the
body of the loop at least once before testing the condition.
Syntax:
do
{
Body of the loop;
} while(condition);
Next statement;
In do..while loop the test condition is checked at the end of the
loop. So it is called exit-controlled loop.
The while condition in the do..while should be followed by
semicolon.
Entry
True
Test Condition
False
Next Statement
1. Execute the body of the loop.
2. check the test condition
if true
goto step 1
else
goto next statement.
/* Example for do..while statement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf("Hello\n");
i++;
}while(i<=5);
getch();
}
/* To count the number of alphabets in a sequence of characters
*/
#include<stdio.h>
void main()
{
int count=0;
char ch;
printf("Enter sequence of characters. Type $ to end:");
do
{
ch=getchar();
if((ch>='A'&& ch<='Z')||(ch>='a'&&ch<='z'))
count++;
}while(ch!='$');
printf("Number of characters entered is %d",count);
}
For loop is an entry controlled loop. The for loop is usually
used when the loop is repeated for a known number of times.
Syntax:
condition
evaluated
false
true
statement
increment
NEXT Statement
1. The initialization expression is evaluated first to initialize
the loop variable.
2. The test expression is evaluated to check the condition for
execution
If true the body of the loop is executed.
Then increment/decrement expression is evaluated and goto
step 2.
3. If the test condition is false the loop is terminated and the
next statement is executed.
/* Example for statement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
printf("Inside for loop\n");
getch();
}
/* Factorial using for loop */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1,i;
clrscr();
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
f=f*i;
printf("Factorial of %d is %d",n,f);
getch();
}
RECAP
In this session we have seen the three type of looping
statements
While using the looping statements we should select the
right choice based on the conditions.
RECAP
while(condition)
{
Body of the loop
}
Next statement;
RECAP
do
{
Body of the loop;
} while(condition);
Next statement;
RECAP
The for loop is used when we are sure about the number
of times the loop is to be executed.