What is the purpose of ";" at the end of for loop?

CFor Loop

C Problem Overview


I found the following code:

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;
}

The point of function is to check whether a number is a prime number or not.

I don't understand why the for-loop has ; at the end:

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

Without that, the code doesn't work properly.

What is the explanation?

C Solutions


Solution 1 - C

It means exactly the same as:

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

Solution 2 - C

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

Solution 3 - C

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.

Solution 4 - C

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.

Solution 5 - C

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.

Solution 6 - C

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

Solution 7 - C

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.

Solution 8 - C

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

Solution 9 - C

a for loop will (normally) have a body,

where the body is enclosed in braces { }

However, for a single statement body, the braces are optional.

; is an empty statement.

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

Solution 10 - C

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.

Solution 11 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWien95View Question on Stackoverflow
Solution 1 - CM.MView Answer on Stackoverflow
Solution 2 - CEliah KaganView Answer on Stackoverflow
Solution 3 - CRohn AdamsView Answer on Stackoverflow
Solution 4 - ChaccksView Answer on Stackoverflow
Solution 5 - CFedericoView Answer on Stackoverflow
Solution 6 - Cuser4826588View Answer on Stackoverflow
Solution 7 - CfalsarellaView Answer on Stackoverflow
Solution 8 - CbmpasiniView Answer on Stackoverflow
Solution 9 - Cuser3629249View Answer on Stackoverflow
Solution 10 - CAceView Answer on Stackoverflow
Solution 11 - ChamidiView Answer on Stackoverflow