Difference between For and While Loop in C

This C Tutorial explains the differences between the while and for loop in C.

While Loop

In while loop, condition is checked before execution of each iteration and as condition fails, loop terminates.

While Loop Syntax

 
    /*
     * In while, condition is tested in the beginning of each iteration
     */    
 
    while (condition) {                   
        statements;                                   
    }

For Loops in C

A for loop is a type of loop that is used in many programming languages. A for loop is similar to a while loop, but the main difference is that a for loop has an initializer, condition, and increment/decrement.

The syntax of a for loop in C programming is:

advertisement
advertisement
for (initialization; condition; adjustment)
{ 
    statements; 						
}

The initializer is used to initialize the loop control variable. The condition is checked each time before execution of the statements. If the condition evaluates to true, then the statements are executed. And if the condition evaluates to false, then execution of the statements is terminated. The increment/decrement step can be used to change the value of the control variable each time through the loop.

Basically, there are two major differences between these two loops.

1. In for loop, initialization, condition and adjustment statements are all put together in one line which make loop easier to understand and implement. While in the while loop, initialization is done prior to the beginning of the loop. Conditional statement is always put at the start of the loop. While adjustment can be either combined with condition or embedded into the body of the loop. For example:

Note: Join free Sanfoundry classes at Telegram or Youtube
    /*
     * for loop
     */
 
    for (initialization; condition; adjustment) {
 
    }
 
    /*
     * while loop
     */
 
    initialization;
    while (condition) {
        statements;
        adjustment;
    }

Example:

#include <stdio.h>
 
int main(void)
{
    int count;
 
    count = 0;
 
    /*
     * adjustment is put together with condition
     */
 
    while (count < 10 && (count += 1))
        printf("count is %d\n", count);
 
    return 0;
}

2. When using “continue;” statement in for loop, control transfers to adjustment statement while in while loop control transfers to the condition statement. For example:

advertisement
  
    for	(initialization; condition; adjustment) {
        statements;
        continue;
        - - - -;
        statements;
    }
		
    initialization;
    while (condition) {
        statements;
        continue;
        adjustment;
    }   

Examples:

int main(void)
{
    int count;
 
    count = 0;
 
    /*
     * 1. adjustment is with the condition
     * 2. continue; statement transfers control to the
     *    "condition + adjustment" in the first line
     * 3. continue; statement like break; statement should
     *    be put under decision-control statements
     * 4. observe the output of the program
     */
 
    while (count < 10 && (count += 1)) {
        if (count <= 5)
            continue;
 
        printf("count is %d\n", count);
    }
    return 0;
}
int main(void)
{
    int count;
 
    /*
     * 1. adjustment is embedded into the body of the loop
     * 2. continue; statement transfers control to the "condition and not
     *    to the adjustment" in the first line
     * 3. continue; statement like break; statement should be put under
     *    decision-control statements
     * 4. observe the output of the program, how it differs from the previous example
     */
 
    count = 0;
    while (count < 10)) {
        count += 1;
        if (count <= 5)
            continue;
 
        printf("count is %d\n", count);
    }
    return 0;
}

1. continue; statement is only used in loops.
2. This is to note that “continue;” statement if used in loops causes the loop to iterate over again preventing the normal execution after the continue statement.

advertisement

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.