Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

for-loop

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?

like image 825
Wien95 Avatar asked Apr 23 '15 23:04

Wien95


People also ask

What is the end of for loop?

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.

What is the purpose of using a for 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.

DO for loops need an end in Python?

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.

What are the 3 parts of a for loop?

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.


2 Answers

It means exactly the same as:

for(count = 2; zahl % count != 0 && zahl >= count; count++) { } 
like image 104
M.M Avatar answered Sep 28 '22 02:09

M.M


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
like image 23
Eliah Kagan Avatar answered Sep 28 '22 02:09

Eliah Kagan