在 for 循环的末尾使用“ ;”的目的是什么?

我找到了以下代码:

int func_prim (int zahl) {
int count;
if (zahl < 0)
return -1;


for (count = 2; zahl % count != 0 && zahl >= count; count++);
if (count == zahl)
return 1;
return 0;
}

函数的作用是检查一个数字是否是质数。

我不明白为什么 for 循环的结尾有 ;:

                                                            v
for (count = 2; zahl % count != 0 && zahl >= count; count++);

没有这个,代码就不能正常工作。

解释是什么?

8107 次浏览

The semicolon at the end of the for-loop means it has no body. Without this semicolon, C thinks the if statement is the body of the for loop.

The for loop is there just to increase the value of count.

a for loop will (normally) have a body,

where the body is enclosed in braces { }

但是,对于单个语句体,大括号是可选的。

; is an empty statement.

Combining the above it becomes obvious that the for loop executes until the condition becomes false.

The ; after the for loop simply means that the for loop won't do anything more than increase the counter count.

The for loop is basically looping through all the numbers that are less than or equal to zahl but greater than 2 and storing it in the variable count. As it loops through all these numbers it is checking to see if zahl is divisible by count. If zahl is divisible by count, the loop is stopped. Otherwise, the loop is stopped when count equals zahl.

The if statement after the for loop checks to see if count is equal to zahl. If it is, then that must mean that the loop went through all the numbers less than zahl and greater than 2. This means that zahl is divisible by all the numbers less than itself and greater 2, which makes zahl prime.

It means exactly the same as:

for(count = 2; zahl % count != 0 && zahl >= count; count++)
{
}

A for loop has the for keyword, followed by parentheses containing three optional expressions separated by semicolons, followed by a body which executes in each iteration of the loop.

The goal of the for loop in your example is to set the value of count, which will be compared to zahl in the if statement that follows. This is achieved in the semicolon-delimited expressions, so the loop body doesn't need to do anything.

Since the loop doesn't need to do anything, it uses the empty statement as its body.

If the ; at the end were omitted and no other changes were made, then the if statement after the for loop would itself become the body of the for loop. (That is not intended and would break the program, as you have observed.)

However, making one's loop body consist of a single ; on the same line is not the only way to write an empty loop body, nor is it probably the most sensible way to do so. It works perfectly well, but the problem is that other readers - and perhaps the same programmer, returning to the project later - may wonder if it was actually an error. After all, one types semicolons at the ends of lines quite often when coding in a C-style language, so it's easy to type an extra one where it doesn't belong.

The other problem is that, in code where a one-line loop with ; as its body is the chosen style, it is difficult to recognize when someone actually has made the mistake of putting a ; when one doesn't belong.

Therefore, these alternatives may be preferable:

  • putting the ;, indented, on the next line -- as sh1 suggests
  • writing the loop body as an empty block, { }, rather than an empty statement
  • making the loop body a continue; statement, which simply causes the loop to move on to its next iteration (which is the same as what happens when the loop body is empty) -- also as sh1 suggests

In addition to what the other excellent answers already say, I would like to point out that

for(count=2; zahl % count != 0 && zahl >= count; count++);

(that is, a for loop with an empty statement used to increment a "counter") is equivalent to

count=2;
while(zahl % count != 0  && zahl >= count)
{
count++;
}

that would make the objective of the code even clearer than some of the listed alternatives: if not comments are present, as in the presented case, a loop with an empty statement might confuse another programmer that has to mantain or use the code (as was the case with the OP here).

The context might help discerning the true scope of the statement, but between a for loop with an empty statement and a while loop with a statement, the latter requires less work to understand its scope.

Syntax of for loop (iteration statement) is

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

statement can be a null statement (;). C11 6.8.3 says

A null statement (consisting of just a semicolon) performs no operations.

In para 5 it gives an example

In the program fragment

char *s;
/* ... */
while (*s++ != '\0')
;

a null statement is used to supply an empty loop body to the iteration statement.

Same thing is happening in

for (count = 2; zahl % count != 0 && zahl >= count; count++);

; is used to supply an empty loop body to the for statement. Without ; the statement next to the for loop will be considered as its body and will be executed.

for Statement:

The for statement is a loop statement whose structure allows easy variable initialization, expression testing, and variable modification. It is very convenient for making counter-controlled loops. Here is the general form of the for statement:

 for (initialize; test; step)
statement

[...]

Null Statement:

The null statement is merely a semicolon alone.

 ;

A null statement does not do anything. It does not store a value anywhere. It does not cause time to pass during the execution of your program.

Most often, a null statement is used as the body of a loop statement, or as one or more of the expressions in a for statement. Here is an example of a for statement that uses the null statement as the body of the loop (and also calculates the integer square root of n, just for fun):

 for (i = 1; i*i < n; i++)
;

Here is another example that uses the null statement as the body of a for loop and also produces output:

 for (x = 1; x <= 5; printf ("x is now %d\n", x), x++)
;

A null statement is also sometimes used to follow a label that would otherwise be the last thing in a block.


In your case, the ; is the Null Statement of the for Statement:

int func_prim (int zahl) {
int count;
if (zahl < 0)
return -1;


for (count = 2; zahl % count != 0 && zahl >= count; count++)
;
if (count == zahl)
return 1;
return 0;
}

Without it, the if becomes the for statement:

int func_prim (int zahl) {
int count;
if (zahl < 0)
return -1;


for (count = 2; zahl % count != 0 && zahl >= count; count++)
if (count == zahl)
return 1;
return 0;
}

Therefore, behaving differently.

It indicates the statement the for loop is used for. It can't be empty. At least it should include a single statement. ; is the empty statement which the iteration is done for.