Loop in C | for, while and do while loop with examples
In this page you will learn about What is Loop in C programming? How many types of Loop in C programming? What is Data Flow Diagram of Loop in C programming? What are examples of Loop in C programming? After reading this page you completely able to define about Loop in C programming languages.
Loop in C language is the one of the most useful looping making statement in real time programming. It gives ability to perform a set of instruction repeatedly. This involves repeating some portion of the program either a specified number of times or until the certain condition is being satisfied. This repetitive operation is done through a loop control structure. It reduces lengthy code in its simple form and the work load of programmer.
Types of Loop:
Types of Loop in C
1. Entry Control Loop
Entry Control Loop means that the condition is checked first and then execute the body of loop. while Loop and for Loop comes under it.
while Loop
while Loop is type of Entry Control Loop. It simple type of loop. It is a loop that repeat a set of statement based upon a condition. As far as loop returns Boolean value of True, the statements inside the body of loop will keep repeating. It also reduces the code and load of programmer. It makes program easy and simple to understand. We use this type of loop when we don't know the exact number of times a code need to be executed.
int main()
{
int n,i=1;
printf("Enter the valid no. to find its table:\n");
scanf("%d",&n);
printf("\nTable of %d:\n", n);
while(i<=10)
{
printf("\n %d * %d = %d", n, i, i*n);
i++;
}
return 0;
}
Enter the valid no. to find its table:
8
Table of 8:
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
for Loop
for(initialization expr; Condition expr; Update expr )
{
// statements that we want to execute
}
Data Flow Diagram of for Loop
int main()
{
int i;
printf("List of alphabet from a to z:");
for(i=97;i<=122;i++)
{
printf(" %c ",i);
}
return 0;
}
Lists of alphabet from a to z:
a b c d e f g h i j k l m n o p q r s t u v w x y z
2. Exit Control Loop
Exit Control Loop means that the body of the loop executed once before checking the condition. Do while Loop comes under it.
do while Loop
do while Loop is a type of Exit Control Loop. It is similar to while loop with one important difference is that it executes its codes once before checking the condition expression. Since do while loop is executed at least once.
do
{
// statements that we want to execute
// increment
}
while(Condition expr);
Example: Write a C program to find factorial of entered number by user using do while loop.
Example: Write a C program to find factorial of entered number by user using do while loop.
int main()
{
int fact=1, num, temp;
printf("Enter the nubmer to find its factorial:\n");
scanf("%d",&num);
temp=num;
do
{
fact=fact*num;
num--;
}
while(num>0);
num=temp;
printf("Factorial of entered no. %d is %d",num, fact);
}
Enter the nubmer to find its factorial:
7
Factorial of entered no. 7 is 5040
Conclusion:
In this article, we have thoroughly read about Loop in C programming with brief description of each types of Loop in C programming. Types of Loop in C programming are:
Exit Control Loop
- while loop
- for loop
Entry Control Loop
- do while loop
If you are interested then also read:
Top 10 C program with output for practice
Flowchart and its different symbol used in making Data Flow Diagram in C
If you find this page helpful then please share!
Tags: Loop in C, Loop in C programming, Types of Loop in C, Data flow diagram of Loop in C, Examples of Loop in C, Example programs of Loop in C
No comments: