c-Looping-Statements
c-Looping-Statements
Note
Unbounded looping statements are used when we don’t know
how many times the set of statements has to be repeat
A do-while loop is similar to the while loop except that the condition
is always executed after the body of a loop. It is also called an exit-
controlled loop.
The basic format of while loop is as follows:
Do
{ statements }
while (expression);
As we saw in a while loop, the body is executed if and only if the
condition is true. In some cases, we have to execute a body of the
loop at least once even if the condition is false. This type of operation
can be achieved by using a do-while loop.
In the do-while loop, the body of a loop is always executed at least
once. After the body is executed, then it checks the condition. If the
condition is true, then it will again execute the body of a loop
otherwise control is transferred out of the loop.
Similar to the while loop, once the control goes out of the loop the
statements which are immediately after the loop is executed.
The critical difference between the while and do-while loop is that in
while loop the while is written at the beginning. In do-while loop, the
while condition is written at the end and terminates with a semi-
colon (;)
For loop
A for loop is a more efficient loop structure in 'C' programming. The
general structure of for loop is as follows:
for (initial value; condition; incrementation or decrementation )
{ statements; }
The initial value of the for loop is performed only once.
The condition is a Boolean expression that tests and compares the
counter to a fixed value after each iteration, stopping the for loop
when false is returned.
The incrementation/decrementation increases (or decreases) the
counter by a set value.
• The nesting of for loops can be done up-to any level. The nested
loops should be adequately indented to make code readable. In some
versions of 'C,' the nesting is limited up to 15 loops, but some provide
more.
• The nested loops are mostly used in array applications
Example programs
Write a program to print the FIBONACCI SERIES upto N Numbers PASCAL TRIANGLE
#include<stdio.h>
#include<stdio.h>
void main()
{ void main()
int n,i,f1,f2,f3; {
printf("enter the number = "); int i,j;
scanf("%d",&n); clrscr();
f1=-1; for(i=1;i<=5;i++)
f2=1; {
for(i=0;i<=n;i++) for(j=1;j<=i;j++)
{
{
f3=f1+f2;
printf("%d\n",f3); printf("%d\n",i);
f1=f2; }
f2=f3; printf("\n");
} }
}
Output
1
Enter the number = 5
22
0
1 333
1
2 4444
3
5 55555
PASCAL TRIANGLE PASCAL TRIANGLE
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int i,j; int i,j,c=1;
clrscr(); clrscr();
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
for(j=1;j<=i;j++) for(j=1;j<=i;j++)
{ {
printf("%d\n",j); printf("%d\n",c);
} c++;
printf("\n"); }
} printf("\n");
}
Output Output
1 1
12 23
123 456
1234 7 8 9 10
12345 11 12 13 14 15
PASCAL TRIANGLE to print *
#include<stdio.h> PASCAL TRIANGLE
void main() #include<stdio.h>
{ void main()
int i,j; {
clrscr(); int i,j;
for(i=1;i<=5;i++) clrscr();
{ for(i=0;i<5;i++)
for(j=1;j<=i;j++) {
{ for(j=i;j>0;j--)
printf(“*"); {
} printf("%d\n",j);
printf("\n"); }
} printf("\n");
}
Output
Output
* 1
21
**
321
** *
4321
** * *
54321
** * * *
To print the word in reverse
#include<stdio.h>
#include<conio.h>
#define size 10
void main()
{ Enter Any String : raja
char name[size+1];
The Given String is raja
int i=1; The Reversed String is ajar
clrscr();
printf("\n Enter Any String");
scanf("%s",name);
printf("\n The Given string is %s\n",name);
for(i=0;name[i]!='\0';i++);
printf("\n\n The Reversed String is");
for(i=size-1;i>=0;i--)
{
printf("%c",name[i]);
}
getch();
}
EXERCISES
10. Write a program to print the following Outputs using for while and do..while loops?
Which loop to Select?