for Statement – for Loop in C Programming Language

In while and do while statement we need to write logic to repeatedly execute a block of statement by initializing a counter and incrementing it after a particular steps. This sometime looks tedious and decreases the readability of the program. The readability of the C program can be improved by using for statement. Using for statement in C language, we can merge all the three parts i.e. assignment, condition checking and increment/decrementing.

Syntax:

Initialization: setting up a loop counter to initial value
test condition: Testing the loop counter to determine whether the loop needs to be executed or not.
increment/decrement: Increment or decrement the loop counter value

Explanation:
The for loop in C is executed as follows:

  1. The initial counter value is initialized. This initialization is done only once for the entire for loop
  2. After the initialization, test condition is checked. Test condition can be any relational or logical expression. If the test condition is satisfied i.e. the condition evaluates to true then the block of statement inside the for loop is executed
  3. After the execution of the block of statement, increment/decrement of the counter is done. After performing this, the test condition is again evaluated. The step 2 and 3 are repeated till the test condition returns false
  4. The example for while loop in C (while Loop in C) can re-written using for loop as follows:

    Output:

    In the above program, value 1 is assigned to i. The for loop would be executed till the value of i is less than or equal to 5.
    Note:It is necessary to write the semicolon in for loop as shown in the below:

    Consider,

    In the above program, Increment is done within the body of for loop. Still semicolon is necessary after the test condition.

    Consider,

    In the above program, Initialization is done before the start of for loop. Still semicolon is necessary before the test condition.

    Note: We can initialize multiple variables in initialization section but only one test condition is allowed.

You may also like...

39 Responses

  1. ativir says:

    i ant prog. for o/p like
    * * *
    *
    * * *
    and
    7 8 9
    7 8
    7

  2. Anonymous says:

    for(i=0; i<10;){
    printf(

  3. Anonymous says:

    /* program for 7 8 9
    7 8
    7 */

    #include
    #include
    void main()
    {
    int i,j,n;
    clrscr();
    for(i=1; i< =3;i++)
    {
    n=7;
    for(j=i; j< =3;j++)
    { printf(“%d”,n);
    n++;
    }
    }
    getch();
    }

  4. Anonymous says:

    /* program for 7 8 9
    7 8
    7 */

    #include
    #include
    void main()
    { int i,j,n;
    clrscr();
    for(i=1;i< =3;i++) {
    n=7;
    for(j=i;j< =3;j++)
    { printf(“%d”,n);
    n++;
    }
    printf(“\n”);
    } getch();
    }

  5. Anonymous says:

    the program for 7 8 9 is not excutinf Turbo C
    please provide me the Correct answer…

  6. Anonymous says:

    No that correct sorry and can please explain each line how it is excuting

  7. Anonymous says:

    * * *
    * *
    *
    how to get this output

  8. LearnCOnline says:

    Hello,

    You can work on the below logic:

    for(i=2; i>=0; i–){
    for(starsperline = 3; starsperline>=1; starsperline –){
    print *;
    }
    print new line;

    }

    The above is just an logic… correct the syntax and run the program. Any issues let me know…

    Thanks,
    LearnCOnline

  9. Anonymous says:

    if(i=1;i< =5;i++)
    what will be the o/p?

  10. Anonymous says:

    can you please put some theory about..Files and files handling modes,, Unions more theories about loop…..

  11. ajay says:

    i want coding for finding gcd of three numbers…

  12. Anonymous says:

    i have an doubt for the program printing *
    in ur program actually starsperline must be depend upon i something like starperline= i+1. i am not sure but have an doubt

  13. Anonymous says:

    i want coding from for loop 1 9 2 8 3 74 6 5 5 6 4 7 3 8 2 9 1
    this count 1 to 9 and in back word 9 to 1….do you know how?
    i wait your answer……………………?

  14. Kamboj says:

    use this code to get output as 192837465564738291:——->

    #include
    #include
    void main()
    {
    int i,j;//declare two variables
    clrscr();//to clear previous result
    for(i=1,j=9;i< =9,j>=1;i++,j–)//i will execute 1 to 9 and j will 9 to 1
    {
    printf(“%d%d”,i,j);//print the result of i and j
    }
    getch();//to hold the output screen
    }

  15. sir i want to know that what is difference in use do ,for and while statement/case please tell me i am waiting for your answer.

  16. Naomi Maina says:

    I want a code to appear in this format
    54321
    5432
    543
    54
    5

    by use of C program

  17. Christopher says:

    @Naomi: how about this?

    #include

    void main(void)
    {
    int i, j;
    int k = 1;

    for (i = 1; i < = 5; i++) {
    for (j = 5; j >= k; j–) {
    printf(“%d”, j);
    }
    k++;
    printf(“\n”);
    }
    }

  18. how this program is execute tell me in detail
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5
    using for loop

  19. Anonymous says:

    thank u Kamboj.iam new to this.your explained line by line.i want out put like this
    1
    2 1 2
    3 2 1 3
    can u explain line by line coding of above out put

  20. Anonymous says:

    @Christopher
    without taking k variable we can write code for
    54321
    5432
    543
    54
    5
    #include
    #include
    void main()
    {
    int i,j;
    clrscr();
    for(i=1;i< =5;i++)
    {
    for(j=5;j>=i;j–)
    printf(“%d”,j);
    printf(“\n”);
    }
    getch();
    }

  21. Md Sumon says:

    try this code, that will help :
    #include
    int main()
    {

    int i,j;
    for(i=1;i< =5;i++){ for(j=1;j< =i;j++)
    //(i=1,j=9;i< =9,j>=1;i++,j–)//
    printf(“%d \n”,j);
    }
    }

  22. Anonymous says:

    @abhishek kumar :

    for(i=1;i< =5;i++){
    for(j=1;j< =i;j++){
    printf(“%d”,i);
    }
    printf(“\n”);
    }

  23. Vishal13 says:

    write program using for loop?
    * * *
    *
    * * *

  24. lala says:

    which codes should I use to have the output of:

    1x2x3x4x5x6x7x8x9x10

    and to get its factorial?

  25. viper01 says:

    give me an example of if statement

  26. surbhi says:

    1
    2 4
    3 6 9
    4 8 12 16
    5 10 15 20 25

  27. M.Yassin says:

    viper0
    astlem 🙂
    #includ
    main()
    {
    int i,g;
    i=2;
    g=12;
    if(i==g)
    printf(“its not right ofcorse”);
    if(i<g)
    printf("that is rigth !\n");
    }

  28. M.Yassin says:

    lala
    take this 🙂
    #include
    int main()
    {
    int i,j,fact=1;
    for(i=1,j=1;i<11,j<11;i++,j++)
    {
    printf("%d*",i); \\this line for print the factory
    fact *=j;
    }
    printf("\n");
    printf("fact=%d",fact);
    }

  29. M.Yassin says:

    Surbhi
    >>
    #include
    main()
    {
    int i,j,k;
    for(i=2,k=1;i<7,k<6;i++,k++)
    {
    for(j=1;j<i;j++)
    printf("%d\t",j*k);
    printf("\n");
    }
    }

    • how about this..please help me.
      Write a program that asks the user to type 5 random integers below 10 and identify the largest value.

      • learnconline says:

        You can use the below logic.
        Accept 5 integers from the user and store it in an array. Once you have the data in an array you can apply sorting algorithm on it. Sort an array in ascending or descending order. If sorted in ascending order, the last element would be largest.
        Thanks,
        Aptuts Team.
        http://www.aptuts.com
        Online Webinar C Coaching

  30. zubair alam says:

    how to get this output
    *
    **
    ***

  31. boy tuklaw says:

    how about this

    1
    234
    5678
    876
    543
    1

  1. September 20, 2015

    […] for Loop […]

Leave a Reply

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