banner



How To Make A Loop

C for Loop

In this tutorial, you lot volition learn to create for loop in C programming with the aid of examples.

Video: C while Loop

In programming, a loop is used to repeat a block of code until the specified condition is met.

C programming has iii types of loops:

  1. for loop
  2. while loop
  3. do...while loop

We will learn well-nigh for loop in this tutorial. In the adjacent tutorial, we will acquire about while and practise...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 initialization statement is executed just one time.
  • So, the examination expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.
  • However, if the examination expression is evaluated to true, statements inside the body of the for loop are executed, and the update expression is updated.
  • Again the exam expression is evaluated.

This procedure goes on until the test expression is false. When the examination expression is faux, the loop terminates.

To learn more about examination expression (when the test expression is evaluated to true and faux), check out relational and logical operators.


for loop Flowchart

Flowchart of for loop in C programming
Working of for loop

Instance 1: for loop

            // Impress numbers from 1 to 10 #include <stdio.h>  int master() {   int i;    for (i = 1; i < 11; ++i)   {     printf("%d ", i);   }   return 0; }                      

Output

            one ii 3 4 5 6 7 eight 9 ten          
  1. i is initialized to 1.
  2. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for loop is executed. This volition impress the one (value of i) on the screen.
  3. The update statement ++i is executed. Now, the value of i volition be two. Again, the test expression is evaluated to true, and the body of for loop is executed. This will impress two (value of i) on the screen.
  4. Again, the update argument ++i is executed and the test expression i < 11 is evaluated. This procedure goes on until i becomes xi.
  5. When i becomes 11, i < eleven volition be false, and the for loop terminates.

Instance 2: for loop

            // Program to calculate the sum of first n natural numbers // Positive integers 1,2,3...n are known every bit natural numbers  #include <stdio.h> int main() {     int num, count, sum = 0;      printf("Enter a positive integer: ");     scanf("%d", &num);      // for loop terminates when num is less than count     for(count = one; count <= num; ++count)     {         sum += count;     }      printf("Sum = %d", sum);      render 0; }          

Output

            Enter a positive integer: ten Sum = 55          

The value entered by the user is stored in the variable num. Suppose, the user entered 10.

The count is initialized to one and the test expression is evaluated. Since the exam expression count<=num (1 less than or equal to 10) is true, the torso of for loop is executed and the value of sum will equal to one.

And so, the update argument ++count is executed and count will equal to ii. Again, the test expression is evaluated. Since 2 is also less than 10, the test expression is evaluated to truthful and the body of the for loop is executed. Now, sum will equal three.

This process goes on and the sum is calculated until the count reaches 11.

When the count is 11, the test expression is evaluated to 0 (faux), and the loop terminates.

So, the value of sum is printed on the screen.


We will learn about while loop and do...while loop in the next tutorial.

Source: https://www.programiz.com/c-programming/c-for-loop

Posted by: carterpaence.blogspot.com

0 Response to "How To Make A Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel