C

C – Example Of A For Loop In C Programming

A loop is used to execute a series of statements until a given condition returns false.

For loop in C

This is one of the most common loops in C programming.
For loop syntax:

for (initialization; condition test; increment or decrement)
{
       //Statements to be executed repeatedly
}

For loop flow diagram

Step 1: The counter variable is initialized as the first step in the process.
Step 2: The condition is checked in the second step, where the counter variable is tested for the given condition; if the condition returns true, the C statements inside the body of the for loop are executed; if the condition returns false, the for loop is terminated and control exits the loop.
Step 3: Depending on the operation (++ or -), the counter variable is incremented or decremented after successful execution of statements within the body of the loop.

For loop example

#include <stdio.h>
int main()
{
   int i;
   for (i=1; i<=3; i++)
   {
       printf("%d\n", i);
   }
   return 0;
}

Output:

1
2
3

C for loops in various forms

In all of the following examples, I’m using variable num as the counter –
1) Instead of num++, I’m using num=num+1, which is equivalent to num++.

for (num=10; num<20; num=num+1)

2) The initialization part of the loop can be skipped, as shown below; the counter variable is declared before the loop.

int num=10;
for (;num<20;num++)

Although we can skip the initialization part, a semicolon (;) before the condition is required; otherwise, a compilation error will occur.
3) As with initialization, you can skip the increment part, as shown below. In this case, a semicolon (;) is required after the condition logic. In this case, the increment or decrement is performed within the loop.

for (num=10; num<20; )
{
      //Statements
      num++;
}

4) This is also an option. The counter variable is set before the loop and incremented within it.

int num=10;
for (;num<20;)
{
      //Statements
      num++;
}

5) As previously stated, the counter variable can also be decremented. The variable in the following example is decremented each time the loop runs until the condition num>10 returns false.

for(num=20; num>10; num--)

C Nested For Loop

Loop nesting is also possible. To illustrate, consider the following:

#include <stdio.h>
int main()
{
   for (int i=0; i<2; i++)
   {
	for (int j=0; j<4; j++)
	{
	   printf("%d, %d\n",i ,j);
	}
   }
   return 0;
}

Output:

0, 0
0, 1
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3

In the preceding example, we have a for loop inside another for loop, which is known as loop nesting. Two-dimensional array is an example of how we can use nested for loops.

Multiple initializations within a C loop

As shown below, we can have multiple initializations in the for loop.

for (i=1,j=1;i<10 && j<10; i++, j++)

What is the distinction between the above for loop and a simple for loop?
1. It is setting up two variables. Both are separated by a comma (,).
2. It combines two test conditions with the AND (&&) logical operator. To join conditions, use a logical operator such as && or || rather than multiple test conditions separated by commas.
3. In the increment section, there are two variables. Should be separated by a comma.

A for loop with multiple test conditions is illustrated

#include <stdio.h>
int main()
{
   int i,j;
   for (i=1,j=1 ; i<3 || j<5; i++,j++)
   {
	printf("%d, %d\n",i ,j);
   }
   return 0;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button