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:
  1. for loop
  2. while loop
  3. do...while loop
We will learn about for loop in this tutorial. In the next tutorial, we will learn about whileand do...while loop.

for Loop

The syntax of the for loop is:
  1. for (initializationStatement; testExpression; updateStatement)
  2. {
  3. // statements inside the body of loop
  4. }

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:
                  1. while (testExpression)
                  2. {
                  3. // statements inside the body of the loop
                  4. }

                  How while loop works?


                  The while circle assesses the test articulation inside the enclosure ().

                  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:
                            1. do
                            2. {
                            3. // statements inside the body of the loop
                            4. }
                            5. 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

                            1. # Print numbers from 1 to 10
                            2. #include <stdio.h>
                            3. int main() {
                            4. int i;
                            5. for (i = 1; i < 11; ++i)
                            6. {
                            7. printf("%d ", i);
                            8. }
                            9. return 0;
                            10. }
                            Output
                            1 2 3 4 5 6 7 8 9 10

                            Example 2: while loop

                            1. // Print numbers from 1 to 5
                            2. #include <stdio.h>
                            3. int main()
                            4. {
                            5. int i = 1;
                            6. while (i <= 5)
                            7. {
                            8. printf("%d\n", i);
                            9. ++i;
                            10. }
                            11. return 0;
                            12. }
                            Output
                            1
                            2
                            3
                            4
                            5

                            Example 3: do...while loop

                            1. // Program to add numbers until the user enters zero
                            2. #include <stdio.h>
                            3. int main()
                            4. {
                            5. double number, sum = 0;
                            6. do
                            7. {
                            8. printf("Enter a number: ");
                            9. scanf("%lf", &number);
                            10. sum += number;
                            11. }
                            12. while(number != 0.0);
                            13. printf("Sum = %.2lf",sum);
                            14. return 0;
                            15. }
                            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

                            No comments

                            Powered by Blogger.