for loop in C ?


A for loop is a control statement using which the programmer can give instructions to the computer to execute a set of statement repeatedly as long as specified condition is satisfied. It is required to specify how many times a set of statements have to be executed, it is also called counter controlled loop.

Syntax:-


for(exp1;exp2;exp3)
{
...........
...........
}


The first expression : exp 1 contains initialization statements
The second expression: exp 2 contains limit–test expression
The third expression : exp 3 contains updating expression

How for Loop Works

  • Step 1:-
    The initialization statement is executed only once i.e exp1.
  • Step 2:-
    Then, the test expression is evaluated. If the test expression is evaluated to false, then the for loop gets terminated and the control comes out of the loop.
  • Step 3:-
    the next step the condition(Test Expression i.e exp2) is checked if the test expression is evaluated to true, statements inside the body of the for loop are executed,
  • Step 4:-
    After successful execution of statements inside the body of the loop, the update expression (i.e exp3) also called counter variable is updated. incremented or decremented, depending on the operation (++ or –).
  • Step 5:-
    Again the test expression is evaluated.

Example with dry Run Concepts


steps-shineskill

Note: The for loop is always used when we know initial value, final value and updating value. The updating value can be constantly incremented or decremented.

Q.) W.A.P to print 1 to 5 using for loop.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int i; 
 for(i = 1; i <=5 ; i++)
 {     
 printf(“%d\n”, i );     
 }
 getch();
 }
Output
1
2
3
4
5

Program Explanation
  • Step 1:-
    We declared integer variables called i. The initialization statement is executed only once i.e i=1
  • Step 2:-
    Then, the test expression is evaluated. i.e i is less than or equals to 5 or not (i <=5 ) hear condition is true so statements inside the body of the for loop are executed, and it will print the value of i i.e 1
  • Step 4:-
    After successful execution of statements inside the body of the loop, the update expression (i.e i++) means it incremented by 1 now the value of i is 2
  • Step 5:-
    Again the test expression is evaluated. and repeat step 2 till the condition is false
  • Step 5:-
    when the condition is false means the value of i will be 6 then for loop gets terminated and the control comes out of the loop.


Q.) W.A.P to print sum of 1 to n using for loop.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
   int i;
   printf("Enter Number);
   scanf("%d",&n);   
   for(i = 1; i <=n ; i++)
   {     
      s=s+i;    
   }
   printf(“Sum of 1 to n is:-%d\n”,s); 
 getch();
 }
Output
Enter Number:- 5
Sum of 1 to n is:- l5

Program Explanation
  • Step 1:-
    We declared three integer variables called i,s and n.
  • Step 2:-
    hear we initialization s=0 because we will have to sum from 1 to n so it not initialization garwage value
  • Step 3:-A Message will give for entering their own values for n :- Enter a Number:- 5
  • Step 4:-
  • The initialization statement is executed only once i.e i=1
  • Step 5:-
    Then, the test expression is evaluated. i.e i is less than or equals to 5 or not (i <=5 ) hear condition is true so statements inside the body of the for loop are executed, and it will print the value of s after assign value s=i+s
  • Step 6:-
    After successful execution of statements inside the body of the loop, the update expression (i.e i++) means it incremented by 1 now the value of i
  • Step 7:-
    Again the test expression is evaluated. and repeat step 2 till the condition is false
  • Step 8:-
    When the condition is false means the value of i will be 6 then for loop gets terminated and the control comes out of the loop.
  • Step 9:-
    and print value of s i.e Sum of 1 to n is 15


Q.) W.A.P to print table of any number using for loop.
 #include<stdio.h>
 #include<conio.h>
 void main()
 {
  int i,t,n;
  printf("Enter a Number");
  scanf("%d",&n); 
  for(i = 1; i <=10 ; i++)
  {     
     t=t*n;
    printf(“%d\n”, t );     
  }
 getch();
 }
Output
Enter a Number. 2
2
4
6
8
10
12
14
16
18
20

Program Explanation
  • Step 1:-
    We declared three integer variables called i,t and n.
  • Step 2:-A Message will give for entering their own values for n :- Enter a Number:- 2
  • Step 3:-
  • The initialization statement is executed only once i.e i=1
  • Step 2:-
    Then, the test expression is evaluated. i.e i is less than or equals to 10 or not (i <=10 ) hear condition is true so statements inside the body of the for loop are executed, and it will print the value of t after assign value i*n
  • Step 4:-
    After successful execution of statements inside the body of the loop, the update expression (i.e i++) means it incremented by 1 now the value of i is 2
  • Step 5:-
    Again the test expression is evaluated. and repeat step 2 till the condition is false
  • Step 5:-
    when the condition is false means the value of i will be 11 then for loop gets terminated and the control comes out of the loop.

C for loop MCQ Question


Q1. Choose a right C Statement
  1. Loops or Repetition block executes a group of statements repeatedly.
  2. Loop is usually executed as long as a condition is met.
  3. Loops usually take advantage of Loop Counter
  4. All the above.

Answer:- D.

Q2. Loops in C Language are implemented using.?
  1. for
  2. while
  3. do while
  4. All the above.

Answer:- D.


Q3.The C code ‘for(;;)’ represents an infinite loop. It can be terminated by
  1. break
  2. exit
  3. if
  4. else

Answer:- a.


Q5. for is a
  1. keyword
  2. identifier
  3. variable
  4. All the above.

Answer:- a.

Q4. ++ and -- is a
  1. increment or decrement operator
  2. identifier
  3. variable
  4. All the above.

Answer:- a.

Q6.What is the output of while Program.?
int main()
{
   int i = 0;
   for (i=0; i<20; i++)
   {
     switch(i)
     {
       case 0:
         i += 5;
       case 1:
         i += 2;
       case 5:
         i += 5;
       default:
         i += 4;
         break;
     }
     printf("%d  ", i);
   }
   return 0;
}
  1. 16 21
  2. 66 45
  3. 65 69
  4. error

Answer:- a.

Related Query in For Loop what is for loop while loop in c nested loop in c for loop syntax for loop in c w3schools for loop example for loop in c programming examples pdf c++ for loop condition evaluation for loop in c programming for loop in c for loop in c in hindi for loop in c programming questions for loop in cpp for loop in c hackerrank solution for loop in c++ example for loop in c plus plus for loop in c definition c for loop program c for loop examples c for loop questions c for loop pattern c for loop w3schools c for loop not working c for loop multiple variables c for loop increment by 2 c for loop array";