What is Loop and type of loop | What are the looping statements in C | How to use loop in c language
Loop
In PC programming, a circle is an arrangement of guidance s that is ceaselessly rehashed until a specific condition is come to. Regularly, a specific procedure is done, for example, getting a thing of information and evolving it, and afterward some condition is checked, for example, regardless of whether a counter has achieved a recommended number.C for Loop
Circles are utilized in programming to execute a square of code over and over until a predetermined condition is met. In this instructional exercise, you will figure out how to make for circle in C programming.
C programming has three types of loops:
- for loop
- while loop
- do...while loop
We will learn about
for
loop in this tutorial. In the next tutorial, we will learn about while
and do...while
loop.for Loop
The syntax of the
for
loop is:
for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}
How for loop works?
The introduction articulation is executed just once.
At that point, the test articulation is assessed. On the off chance that the test articulation is assessed to false, the forloop is ended.
Be that as it may, if the test articulation is assessed to genuine, proclamations inside the collection of forloop are executed, and the update articulation is refreshed.
Again the test articulation is assessed.
This procedure goes on until the test articulation is false. At the point when the test articulation is false, the circle ends.
while loop
The syntax of the
while
loop is:
while (testExpression)
{
// statements inside the body of the loop
}
How while loop works?
On the off chance that the test articulation is valid, explanations inside the assemblage of while circle are executed. At that point, the test articulation is assessed once more.
The procedure goes on until the test articulation is assessed to false.
On the off chance that the test articulation is false, the circle ends (closes).
do...while loop
The
do..while
loop is similar to the while
loop with one important difference. The body of do...while
loop is executed at least once. Only then, the test expression is evaluated.
The syntax of the
do...while
loop is:
do
{
// statements inside the body of the loop
}
while (testExpression);
How do...while loop works?
- The body of do...while loop is executed once. Only then, the test expression is evaluated.
- If the test expression is true, the body of the loop is executed again and the test expression is evaluated.
- This process goes on until the test expression becomes false.
- If the test expression is false, the loop ends.
Example 1: for loop
# Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
Example 2: while loop
// Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d\n", i);
++i;
}
return 0;
}
Output
1 2 3 4 5
Example 3: do...while loop
// Program to add numbers until the user enters zero
#include <stdio.h>
int main()
{
double number, sum = 0;
do
{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
Output
Enter a number: 1.5 Enter a number: 2.4 Enter a number: -3.4 Enter a number: 4.2 Enter a number: 0 Sum = 4.70
Post a Comment