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 75785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:34:14+00:00 2026-05-10T20:34:14+00:00

I have seen some very weird for loops when reading other people’s code. I

  • 0

I have seen some very weird for loops when reading other people’s code. I have been trying to search for a full syntax explanation for the for loop in C but it is very hard because the word ‘for’ appears in unrelated sentences making the search almost impossible to Google effectively.

This question came to my mind after reading this thread which made me curious again.

The for here:

for(p=0;p+=(a&1)*b,a!=1;a>>=1,b<<=1); 

In the middle condition there is a comma separating the two pieces of code, what does this comma do? The comma on the right side I understand as it makes both a>>=1 and b<<=1.

But within a loop exit condition, what happens? Does it exit when p==0, when a==1 or when both happen?

It would be great if anyone could help me understand this and maybe point me in the direction of a full for loop syntax description.

  • 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. 2026-05-10T20:34:15+00:00Added an answer on May 10, 2026 at 8:34 pm

    The comma is not exclusive of for loops; it is the comma operator.

    x = (a, b); 

    will do first a, then b, then set x to the value of b.

    The for syntax is:

    for (init; condition; increment)     ... 

    Which is somewhat (ignoring continue and break for now) equivalent to:

    init; while (condition) {     ...     increment; } 

    So your for loop example is (again ignoring continue and break) equivalent to

    p=0; while (p+=(a&1)*b,a!=1) {     ...     a>>=1,b<<=1; } 

    Which acts as if it were (again ignoring continue and break):

    p=0;  while (true) {     p+=(a&1)*b;     if (a == 1) break;     ...     a>>=1;     b<<=1; } 

    Two extra details of the for loop which were not in the simplified conversion to a while loop above:

    • If the condition is omitted, it is always true (resulting in an infinite loop unless a break, goto, or something else breaks the loop).
    • A continue acts as if it were a goto to a label just before the increment, unlike a continue in the while loop which would skip the increment.

    Also, an important detail about the comma operator: it is a sequence point, like && and || (which is why I can split it in separate statements and keep its meaning intact).


    Changes in C99

    The C99 standard introduces a couple of nuances not mentioned earlier in this explanation (which is very good for C89/C90).

    First, all loops are blocks in their own right. Effectively,

    for (...) { ... } 

    is itself wrapped in a pair of braces

    { for (...) { ... } } 

    The standard sayeth:

    ISO/IEC 9899:1999 §6.8.5 Iteration statements

    ¶5 An iteration statement is a block whose scope is a strict subset of the scope of its enclosing block. The loop body is also a block whose scope is a strict subset of the scope of the iteration statement.

    This is also described in the Rationale in terms of the extra set of braces.

    Secondly, the init portion in C99 can be a (single) declaration, as in

    for (int i = 0; i < sizeof(something); i++) { ... } 

    Now the ‘block wrapped around the loop’ comes into its own; it explains why the variable i cannot be accessed outside the loop. You can declare more than one variable, but they must all be of the same type:

    for (int i = 0, j = sizeof(something); i < j; i++, j--) { ... } 

    The standard sayeth:

    ISO/IEC 9899:1999 §6.8.5.3 The for statement

    The statement

    for ( clause-1 ; expression-2 ; expression-3 ) statement 

    behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any variables it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression.133)

    Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant.

    133) Thus, clause-1 specifies initialization for the loop, possibly declaring one or more variables for use in the loop; the controlling expression, expression-2, specifies an evaluation made before each iteration, such that execution of the loop continues until the expression compares equal to 0; and expression-3 specifies an operation (such as incrementing) that is performed after each iteration.

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

Sidebar

Ask A Question

Stats

  • Questions 112k
  • Answers 112k
  • 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 could set a large minimum width for the line… May 11, 2026 at 9:53 pm
  • Editorial Team
    Editorial Team added an answer strncpy copies at most - in this case - sizeof… May 11, 2026 at 9:53 pm
  • Editorial Team
    Editorial Team added an answer That's an odd question... I get the impression that... you… May 11, 2026 at 9:53 pm

Related Questions

I have seen some very weird for loops when reading other people's code. I
I'd like to be able to open a TDataSet asynchronously in its own thread
I have a main application class, which contains a logger, plus some general app
I have a website form that requires a US phone number input for follow
I've seen a lot of questions related to mapping DTOs to Domain Objects, but

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.