Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 602833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:51:54+00:00 2026-05-13T16:51:54+00:00

for() or while() – which is BEST? for (i=1; i<a; i++) /* do something

  • 0

for() or while() – which is BEST?

for (i=1; i<a; i++)
  /* do something */

OR

i=1;
while (i<a) {
/* do something */
i++;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:51:55+00:00Added an answer on May 13, 2026 at 4:51 pm

    The first is the idiomatic way; it is what most C coders will expect to see. However, I should note that most people will also expect to see

    for(i = 0; i < a; i++)
    

    Note that the loop starts at zero. This will do something a times. If you’re going to write a while loop that is equivalent to a for loop as above I strongly encourage you to write it as a for loop. Again, it is what C coders expect to see. Further, as a for loop it is easier to read as everything (initialization, loop condition, expression to be executed after each iteration) are all on one line. For the while loop they are spread out hindering readability.

    Note, however, there are circumstances in which seemingly equivalent for and while loops are actually not. For example:

    for(i = 0; i < 10; i++) {
        if(i == 5) continue;
        printf("%d\n", i);
    }
    

    and

    i = 0;
    while(i < 10) {
        if(i == 5) continue;
        printf("%d\n", i);
        i++;
    }
    

    appear at first glance to be equivalent but they are not. The for loop will print 0--9 skipping 5 on the console whereas the while loop will print 0--4 on the console and then enter an infinite loop.

    Now, that handles the simple case that you asked about. What about the more complex cases that you didn’t ask about? Well, it really depends but a good rule of thumb is this: if you are going to repeat something a fixed pre-determined number of times, a for loop is generally best. Otherwise, use a while loop. This is not a hard-and-fast rule but it is a good rule-of-thumb. For example, you could write

    unsigned int v;
    unsigned int c;
    for(c = 0; v; v >>= 1) c += v & 1;
    

    but I think most C programmers would write this as

    unsigned int v;
    unsigned int c;
    c = 0;
    while(v) { c += v & 1; v >>= 1; }
    

    Or, as another example, if you’re going to read until the end of a file then you should use a while loop. Thus

    FILE *fp;
    fp = fopen(path, "r");
    while(fgets(buf, max, fp) != NULL) { /* something */ }
    

    instead of

    FILE *fp;
    for(fp = fopen(path, "r"); fgets(buf, max, fp) != NULL; ) { /* something */ }
    

    Now, reaching into religious territory, this is why I prefer while(1) as the right way to do an infinite loop over for(;;).

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 430k
  • Answers 430k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You get quite a bit of control here: Windows ->… May 15, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer If you're cross-compiling an executable, you also need to cross-compile… May 15, 2026 at 2:05 pm
  • Editorial Team
    Editorial Team added an answer At the time when GDI+ (the technology which System.Drawing is… May 15, 2026 at 2:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.