How do I use the semicolon in the "for" command?

Asked

Viewed 69 times

2

What’s the difference between a for ending with a semicolon and a for ending without a semicolon?

Example:

for(i=0; i<=10; i++)**;**  //Termina com ponto e vírgula//

Example:

for(i=0; i<=10; i++)  //Termina sem ponto e vírgula//
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

2

The semicolon is a finalizer of statement (plus). It’s not the end of the line or anything else that ends it, only that character does it, no matter where it is, it can even be on another line.

Now we can understand that we have 3 statements within the definition of for (one of the commands that create a loop of repetition), the initialization, the condition, and the step.

Besides, there’s usually another statement following the definition of for which is what must be executed in every repetition that it does.

That one statement can be a block of statements or one in isolation. Or it can still be a statement empty, that is, it does nothing. It may be useful in some cases that the goal is to do nothing beyond what it has already defined.

And in fact in some cases what is in the three (or less, they are not mandatory, although if not using them may be better to use another flow control command) statements within the definition of for can already handle the message and do everything you want, and you don’t need to have something extra to be repeated. In this case like the for always expecting a statement for it to repeat can use a void, ie only put the ; nothing else, and it may be right after the for.

The definition of for in itself does not have a finalizer because it is not a statement complete.

The keys with nothing inside is an empty block, and it works just like an empty block.

Do the same:

for (int i = 0; i < 10; i++);
for (int i = 0; i < 10; i++) ;
for (int i = 0; i < 10; i++)
    ;
for (int i = 0; i < 10; i++) {}
for (int i = 0; i < 10; i++) {
}
for (int i = 0; i < 10; i++) {
    ;
}

I put in the Github for future reference.

Just note that this particular example does not make sense, it does not make something useful and should not be used like this.

The compiler can somehow accept the use of white spaces (which includes tabulation or end of line), but good programmers make their use more meaningful, and think of what is most readable and best passes the intention. Note that I wrote in a different way but become more readable.

The second example of the question will repeat the next statement that find, even in another line and that is not the intention, provided that there is one (if there is no normal is give error).

See more in What is the importance of using the word "this"?.

  • #include<stdio. h> #include<locale. h> int main(){ int matricula[10], j, i=1, aux; setlocale(LC_ALL, "ENGLISH"); printf("Enter 1st matricula: "); scanf("%d", &matricula[0]); printf("%d n", matricula[0]); while(i<10){ printf("Enter %Dª matricula: ", i+1); scanf("%d", &aux); for(j = 0; matricula[j]!= aux; j++) if(j >= i){ matricula[i] = aux; printf("%d n", matricula[i]); i++; } } Return 0; } I don’t understand why it doesn’t work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.