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?
break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.
A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number. When the number of times is not known before hand, we use a "While" loop.
The code block in a for loop (in python) has no curly braces nor an "end" keyword indicating the point where the block terminates. All other languages have the code block wrapped in some way to move execution back to the top for each item.
Similar to a While loop, a For loop consists of three parts: the keyword For that starts the loop, the condition being tested, and the EndFor keyword that terminates the loop.
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:
;
, indented, on the next line -- as sh1 suggests { }
, rather than an empty statementcontinue;
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 If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With