Example HTML page

For Loop in C Programming

Loops are used in programming to repeat a specific block of code.

The syntax of for loop is:

for(initialization;expression;increment/decrement)
{
//statements of for loop
}

Figure: Loop repetition

Working procedure of for loop:

The initialization statement is executed only once.

Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop  is executed and the update expression is updated.

This process repeats until the test expression is false.

Program 1: Printing a message “Hello Sudhakar” 10 times.

#include<stdio.h>
int main()
{
	int i;
	for (i=1;i<=10;i++)
	{
		printf("Hello Sudhakar\n");
	}
}

The variable ‘i’ is declared of integer type. In the for loop ‘i’ value is initialized to 1, and the expression is evaluated, i <=10 is true, hence the print statement is executed. now the modify operator increments the value of i. Now the value of ‘i’ is 2 and expression is evaluated and is true again. This process is repeated until the ‘i’ value becomes 11. 11<=10 is false, the for loop is terminated.

Output:

Command Prompt

Hello Sudhakar
Hello Sudhakar
Hello Sudhakar
Hello Sudhakar
Hello Sudhakar
Hello Sudhakar
Hello Sudhakar
Hello Sudhakar
Hello Sudhakar
Hello Sudhakar

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

Output:

Command Prompt

1

2

3

4

5

6

7

8

9

10

 

Example HTML page

Leave a Reply

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

Pin It on Pinterest