What does the following for loop do? When will the program break out of the loop?
for (c = 0; n; c++) {
/* . . . */
}
Here is an example of such a for loop in this answer on SO.
What does the following for loop do? When will the program break out of the loop?
for (c = 0; n; c++) {
/* . . . */
}
Here is an example of such a for loop in this answer on SO.
The loop will break when n
becomes 0
(in other words, the loop will not stop until n
becomes 0
). This can happen within the loop (the loop body sets n
to 0
eventually), or it could possibly happen because of some external event. Examples of external events would be a shared memory update, or modification by a signal handler. The c
variable is incremented as many times as the loop body is entered by the for
loop. The c
variable is initialized to 0
.
Technically, your code fragment is not a The question title and post has since been modified.for
loop, but just the controlling construct of a for
loop statement. The fragment is missing a loop body. But that's okay, since your question title is about "this for
-loop structure".
You link to a function in a different answer, but you already seem to understand that the purpose of that function is to count the number of set bits in a number.
EDIT: This answer is an explanation of the bitwise operation in the linked question given in error. It still explains the termination at the end.
The linked code is as follows:
long count_bits(long n) {
unsigned int c; // c accumulates the total bits set in v
for (c = 0; n; c++)
n &= n - 1; // clear the least significant bit set
return c;
}
The interesting line here is n &= n - 1;
. This sets n
to n & (n - 1)
, where &
is the bitwise AND operator. The example says it clears the least significant bit, i.e. sets the lowest unit bit to 0, and that's exactly what it does, and here's why.
Imagine this example, we have the number, for example:
01010100
If we decrement (take 1), we get:
01010011
***
Every time we decrement the lowest bit is removed, and bits below become 1. That's just how subtraction works, it's the equivalent of 1000 - 1 = 999 in decimal, but with two digits instead of ten.
Now when we & them together:
01010100
&01010011
=01010000
***
A bitwise & only sets a bit to 1 if both entered bits are 1.
So as you can see the operation always removes the lowest set bit as the lowest bit becomes 0 after subtraction, and the bits below were 0 before subtraction: all these bits fail to have both digits 1 for the & operation, and so evaluate to 0. All other set bits are unchanged, so give two 1s for the operation and keep their value.
The loop terminates when condition n
is false, i.e. when n
is 0
(all the bits are removed, value 00000000
). So this is after each bit is removed, and c
leaves with value of the number of bits that were in n
.
Remember that in C a boolean value is considered 'true' when the number form is non-zero, and 'false' when the number is zero.
The C standard specifies that the "middle thing" in a for
clause is an expression. As long as that expression evaluates to non-zero the loop is continued. So n
is an expression that will lead to the loop being terminated once n
is zero. A more explicit form would be
for (c = 0; n != 0; c++)
which any decent compiler compiles to the same code.
In fact, the C standard allows as a special case, the "middle thing" to be empty, in which case it is considered to be true. This is the reason why we can write infinite loops as
for (;;) { ... }
instead of the ugly as hell abomination while(1)
.
Heh..
[Case 1] - no loop's iterations
int c, n = &c;
for (c = 0; n; c++) {
/* . . . */
}
[Case 2] - no loop's iterations
int c, n = 0;
for (c = 0; n; c++) {
/* . . . */
}
[Case 3] - infinity loop's iterations
int c, n = ...; // any value, except 0
for (c = 0; n; c++) {
/* . . . */
}
Is there another cases?
ps. of course, it's joke..