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?

  1. for (i=1; i < 20; i++) printf ("k");
  2. for (i=1; i = 20; i++) printf ("k");
  3. for (i=0; i < 20; i++) printf ("k");
  4. for (i=0; i <= 20; i++) printf ("k");
Qus. 2 : <p>What is the final value of x when the code is executed?</p><pre><span style="font-size: 14px;">int x; <br></span><span style="font-size: 14px;">for(x=0; x<10; x++) <br></span><span style="font-size: 14px;">{} </span></pre>

  1. 10
  2. 9
  3. 0
  4. 1
Qus. 3 : The following code ‘for(;;)’ represents an infinite loop. It can be terminated by.

  1. break
  2. exit(0)
  3. abort()
  4. All of the mentioned
Qus. 4 : <p>What will be the output of following program?</p> <pre>int main() <br>{<br> <span style="white-space:pre"> </span>for(int c=1;c<5;++c); <br><span style="white-space:pre"> </span>printf(“%d”,c); <br>}</pre>

  1. 1
  2. 5
  3. 6
  4. 12345
Qus. 5 : Which of the following is the correct syntax for a for loop in C?

  1. for (int i = 0; i < 10; i++)
  2. for i = 0 to 10 step 1
  3. for (i = 0; i < 10; i++)
  4. for (i < 10; i++)
Qus. 6 : <p>How many times will the following loop execute?</p><pre><span style="font-size: 14px;">int j;<br></span><span style="font-size: 14px;">for(j = 1; j &lt;= 10; j = j-1)<br></span><span style="font-size: 14px;">printf("INFOMAX");</span></pre>

  1. Forever
  2. Never
  3. 0
  4. 10

Programs

C Program to Calculate the Sum of Natural Numbers

View Solution


C Program to Find Factorial of a Number

View Solution


C Program to Generate Multiplication Table

View Solution


C Program to Display Fibonacci Sequence

View Solution


C Program to Find GCD of two Numbers

View Solution


C Program to Find LCM of two Numbers

View Solution


C Program to Display Characters from A to Z Using Loop

View Solution


C Program to Count Number of Digits in an Integer

View Solution


C Program to Reverse a Number

View Solution


C Program to Calculate the Power of a Number

View Solution


C Program to Check Whether a Number is Palindrome or Not

View Solution


C Program to Check Whether a Number is Prime or Not

View Solution


C Program to Display Prime Numbers Between Two Intervals

View Solution


C Program to Check Armstrong Number

View Solution


C Program to Display Armstrong Number Between Two Intervals

View Solution


C Program to Display Factors of a Number

View Solution


c program to print the sum of negative numbers, sum of positive odd numbers and sum of positive even numbers

View Solution


c program to generate multiplication table for first n number

View Solution


C program to computer the average of every third integer lying between 1 and 200

View Solution


c program to check whether the given number is Perfect Number or not

View Solution


C program to convert a given decimal number to its octal equivalent and vice versa

View Solution


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.

View Solution


Write a program to generate the triangular number

View Solution


Write a program to find the sum of the following series

View Solution


Write a program to find the perfect squares from 1 to 500.

View Solution


Write a program to detect the largest number out of five numbers and display it

View Solution


Write a program to print the five entered numbers in the ascending order

View Solution


Calculate the sum and average of five subjects

View Solution


Calculate the value of mx . Where ‘m’ is a mantissa and ‘x’ is an exponent

View Solution


Write a program to find perfect cubes up to a given number

View Solution


Write a program to display numbers 1 to 100 using ASCII values from 48 to 57

View Solution


Write a program to count number of votes secured by A and B

View Solution


Write a program to count the occurrence of 0 to 9 digits between 1

View Solution


Accept a number and find sum of its individual digits repeatedly till the result is a single digit.

View Solution


Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad