0% found this document useful (0 votes)
7 views

c-Looping-Statements

The document explains the concept of loops in C programming, highlighting their importance for executing statements multiple times efficiently. It details two main types of loops: entry-controlled (while and for loops) and exit-controlled (do-while loop), along with their syntax and usage. Additionally, it provides example programs and guidelines for selecting the appropriate loop type based on the problem requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

c-Looping-Statements

The document explains the concept of loops in C programming, highlighting their importance for executing statements multiple times efficiently. It details two main types of loops: entry-controlled (while and for loops) and exit-controlled (do-while loop), along with their syntax and usage. Additionally, it provides example programs and guidelines for selecting the appropriate loop type based on the problem requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

C programming language, there are circumstances where

you want to do the same thing many times


For example you want to print the same words ten
times.

You could type ten printf function, but it is easier to use


a loop. The only thing you have to do is to setup a loop
that execute the same printf function ten times
A loop is defined as a block of statements, which are
repeatedly executed for a certain number of times
Looping statements are used for running a set of
statements for any number of times
Looping statements are also called as iterative
statements
In C Programming there are mainly two types of loops

Note
Unbounded looping statements are used when we don’t know
how many times the set of statements has to be repeat

Unbounded looping statements can either be a Pre – Test


Loop or be a Post – Test Loop

In Pre – Test Loop, condition is checked before the


beginning of each iteration. If condition is TRUE
repetition is performed, if it is FALSE repetition is not
performed
Pre – Test loop is implemented using ‘while’ statement

In Post – Test Loop, first the block of statements to be


repeat is executed then condition will be tested

That means in Post – Test Loop, condition is checked


after executing the repeating statements, if the condition
is TRUE it repeat the statements again, if it is FALSE
repetition is not performed

Post – Test loop is implemented using ‘do - while’ statement


counter initialization;
while(condition)
{
Syntax block of statements;
…..
counter modification;
…..
}
#include<stdio.h>
int i = 1 void main()
{
i<=10
int i=1;
while(i <= 10)
{
printf….MRIT
i++
printf(“MRIT\n”);
i++;
printf….END
}
printf(“END”);
}
Do-While loop

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?

• Selection of a loop is always a tough task for a programmer, to select


a loop do the following steps:
• Analyze the problem and check whether it requires a pre-test or a
post-test loop.
• If pre-test is required, use a while or for a loop.
• If post-test is required, use a do-while loop.
Summary

• Looping is one of the key concepts on any programming language.


• It executes a block of statements number of times until the condition
becomes false.
• Loops are of 2 types: entry-controlled and exit-controlled.
• 'C' programming provides us 1) while 2) do-while and 3) for loop.
• For and while loop is entry-controlled loops.
• Do-while is an exit-controlled loop.

You might also like