For Loop In C Programming Language

For Loop In C Programming Language

With the aid of examples, you will learn how to write a for loop in C programming in this article.

In programming, there are instances when it is necessary to repeat an operation n times or more. When we need to repeatedly run a set of statements, we utilize loops.

A loop is a programming construct used to iterate across a block of code until the required condition is satisfied.

The three types of loops in C programming are:

  • for loop
  • while loop
  • do…while loop

For loops will be covered in this course. We shall learn about the while and do…while loop in the upcoming tutorial.

for Loop in c

The for loop’s syntax is as follows:

for (initializationStatement; testExpression; updateStatement)
{
    // statements inside the body of loop
}

How does the for loop work in c?

  • There is just one execution of the startup statement.
    The test expression is then assessed.
  • The for loop is ended if the test statement is interpreted as false.
  • But if the test expression is evaluated as true, the for loop’s body statements are carried out, and the update expression is changed.
  • The test expression is assessed once again.
  • Up until the test, the expression is false, this operation is repeated. The loop ends if the test expression returns false.
  • Check out relational and logical operators to find out more about test expression (when the test expression is evaluated to true and false).

Example 1: for loop in c

// 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

  • Initialization sets I to 1.
  • It evaluates the test phrase I 11. The for loop’s body is carried out since 1 less than 11 is a true statement.
  • This will display the number 1 (I’s value) on the screen.
  • It executes the update statement I. I will now have a value of 2. Once more, the test expression is judged to be true, and the for loop’s body is then run. The screen will display 2 (the value of I as a result.
  • Once more, the test expression I 11 is assessed after the update statement I has been executed. This continues until I turn 11 years old.
  • I will be 11 when the for loop ends, and I 11 will be false.

Example 2: for loop in C

// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as 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 = 1; count <= num; ++count)
    {
        sum += count;
    }

    printf("Sum = %d", sum);

    return 0;
}

Output

Enter a positive integer: 10
Sum = 55

The variable num holds the value that the user entered. Let’s say the user typed 10.

The test expression is assessed once the count is initialized to 1.

Since the for loop’s body is run and the value of the sum is equal to 1, the test expression count=num (1 less than or equal to 10) is true.

The update command ++count is then carried out, and count equals 2. The test expression is assessed once again.

Due to the fact that 2 is also less than 10, the test expression is determined to be true and the for loop’s body is run. The sum will now equal 3.

Up until the count reaches 11, this process is repeated and the sum is calculated.

The test expression is evaluated to 0 (false) when the count reaches 11, and the loop ends.

The sum’s amount is then displayed on the screen.

In the following tutorial, we will learn about the while loop and the do…while loop.

Leave a Reply