For Loop
for and while Loops
Looping statement are the statements execute one or more statement repeatedly several number of times. In C programming language there are three types of loops:
- For loop
- While loop
- Do while loop
FOR Loop:
for loop is a statement which allows code to be repeatedly executed. For loop contains 3 parts Initialization, Condition and Increment or Decrements.
Syntax:
for(initial value ;condition; increment/decrement)
{
statement 1
statement 1
……………
statement n
}

Variation in FOR Loop
A for loop can have multiple variations that makes more flexible and efficient.
- Multiple initialization and update expression
- Optional expression
- Infinite loop
- Empty loop
Multiple initialization and update expression
A for loop may contain multiple initialization and update expressions. The multiple expressions must be separated by commas.
Example:
int i,j;
for(i=1,j=20; I,=10; i++,j--)
{
printf("%d %d ",i,j)
}
Optional Expression
By now you are familiar that a for loop contains three types of expressions: initialization expression(condition) and an update expression. But, all these expressions are optional, i.e., we can skip any or all of these expressions depending upon the requirement of the program.
Case 1:
int i=2;
for( ;i<=20;i=i+2)
{
printf("%d",i);
}
Case 2:
int i=2;
for( ;i<=20; )
{
printf(“%d”,i);
i=i+2;
}
Case 3:
int i=2;
for( ; ; )
{
printf(“%d”,i);
i=i+2;
if(i>20)
{
break;
}
}
Infinite Loop
A for loop will execute its statement block infinite number of times if we skip the condition. If the test expression (condition) is not present in the loop, it will never return false. Hence, it will execute endlessly and become an infinite loop.
Case 1
int I
for(i=1; ;i++)
{
pritlf("%d",i);
}
Case 2
int i;
for( ; ; )
{
pritlf("%d",i);
}
Case 3
int i;
for(i=1;i<=i;i++)
{
pritlf("%d",i);
}
Here , i<=I will always be true.
Empty Loop
A loop without any statement in its body is called an empty loop
Case 1:
int i
for(i=2;i<=50;i++)
{
}
Case 2:
for(i=2;i<=50;i++);
Qus. 1 : Which code will print k 20 times?
- for (i=1; i < 20; i++) printf ("k");
- for (i=1; i = 20; i++) printf ("k");
- for (i=0; i < 20; i++) printf ("k");
- for (i=0; i <= 20; i++) printf ("k");
- 10
- 9
- 0
- 1
- break
- exit(0)
- abort()
- All of the mentioned
- 1
- 5
- 6
- 12345
- for (int i = 0; i < 10; i++)
- for i = 0 to 10 step 1
- for (i = 0; i < 10; i++)
- for (i < 10; i++)
- Forever
- Never
- 0
- 10
Programs
C Program to Calculate the Sum of Natural Numbers
C Program to Find Factorial of a Number
C Program to Generate Multiplication Table
C Program to Display Fibonacci Sequence
C Program to Find GCD of two Numbers
C Program to Find LCM of two Numbers
C Program to Display Characters from A to Z Using Loop
C Program to Count Number of Digits in an Integer
C Program to Reverse a Number
C Program to Calculate the Power of a Number
C Program to Check Whether a Number is Palindrome or Not
C Program to Check Whether a Number is Prime or Not
C Program to Display Prime Numbers Between Two Intervals
C Program to Check Armstrong Number
C Program to Display Armstrong Number Between Two Intervals
C Program to Display Factors of a Number
c program to print the sum of negative numbers, sum of positive odd numbers and sum of positive even numbers
c program to generate multiplication table for first n number
C program to computer the average of every third integer lying between 1 and 200
c program to check whether the given number is Perfect Number or not
C program to convert a given decimal number to its octal equivalent and vice versa
Write a program to find the number in between 7 and 100 which is exactly divisible by 4 and if divided by 5 and 6 remainders obtained should be 4.
Write a program to generate the triangular number
Write a program to find the sum of the following series
Write a program to find the perfect squares from 1 to 500.
Write a program to detect the largest number out of five numbers and display it
Write a program to print the five entered numbers in the ascending order
Calculate the sum and average of five subjects
Calculate the value of mx . Where ‘m’ is a mantissa and ‘x’ is an exponent
Write a program to find perfect cubes up to a given number
Write a program to display numbers 1 to 100 using ASCII values from 48 to 57
Write a program to count number of votes secured by A and B
Write a program to count the occurrence of 0 to 9 digits between 1
Accept a number and find sum of its individual digits repeatedly till the result is a single digit.