Go with a ";" at the beginning of the loop, which means

Asked

Viewed 310 times

15

I picked up a code with a code like this:

for (; indField > 0 && indMask > 0; )

What does that mean ";" at the beginning and end of the command?

  • 2

    The ideal would be to check places where indField and indMask variables are used previously. I can not test at the moment, but I believe it is the following: The for has the structure (initialization; condition; increment), in his example he has only the condition.

2 answers

30


The for has 3 "parts":

for ( executar antes de começar ; condição para executar ; executar ao fim da iteração )

You only need to fill in what you need. But you have to put the ; anyway.

In this case, the author of the code did not need to do anything to initialize the loop, only interested him the condition to iterate, which is the middle item.

Likewise, if he wanted a loop infinite, it would be enough to also omit the condition:

for( ;; ) {
   // ficarei em loop até o fim dos tempos (ou alguma coisa externa me parar)
}

15

The person should have used one while in this case, after all has only one condition and nothing else. The wisest would be:

while (indField > 0 && indMask > 0)

In that case the structure should not be a for Because you don’t use what he has to your advantage. The confusion of the AP that originated the question was precisely because the programmer did not follow the basic precept of doing what is most semantically correct, which is what he and everyone should do. At the very least is more elegant use the while there.

Technically, it means you didn’t want to initialize a variable, as is common to do in a for. also did not want to execute anything at every step of loop iteration, noted that there is nothing also after the last ;?.

A for is:

for (           //o comando
    int i = 0;  //a inicialização da variável (geralmente, pode ser qualquer ação)
    i < 10;     //a condição de término do laço (sempre tem que ser um resultado booleano
    i++         //o passo a executar em cada interação, o incremento é muito comum
) {             //fecha o comando e abre o bloco de execução

Note that the for is always composed of 3 stocks different and then usually have a block of what should be executed in the loop. In theory all 3 parts can be omitted depending on what you want.

Other things that few people know is that there can be more than one expression in each of these parts, separating by commas, and that’s not unique to for, is valid in any context where a statement. Example:

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

I put in the Github for future reference.

Even the condition can do this, but only the last boolean expression will be considered as the final result to determine whether the loop should continue or not.

So each of the 3 statements of for can have from zero to "infinite" expressions.

Behold What’s the difference between while, for, while and foreach?. It’s another language, but it’s worth the same.

  • Well remembered to warn that it is case of while.

  • 2

    Someone did not like and negatived. I think the person should like to use the for where the while better :)

  • 1

    His response was partly based on opinion, and not on focus, I think judging programmers is not the focus of the site.

  • @Marcoviniciussoaresdalalba having opinion is not a problem, the answer cannot be based on opinion and what has been answered is correct and not based on opinion. Voting is opinionated. Being focused and not being able are separate things. The focus of the answer is not opinionated. Actually, even the point you’re making is not opinionated. I have given two possibilities, either the person does not know what is most suitable or she did it on purpose because she likes to do "different", there is no other option. I didn’t say what the person who did it is, so I didn’t judge the person, I said about the choice of action.

  • @Marcoviniciussoaresdalalba but I accept suggestions, if you have a valid third option that I have forgotten to justify being used as while, comment, that if it is sensible thing I can improve the answer. Comment that adds value is always good.

  • And your comment that because you received downvote judges who gave the downvote? You may not have the intention to judge but you are doing this.

  • 3

    @Marcoviniciussoaresdalalbasim, you’re judging, you’re giving your opinion, and I don’t complain about that, that’s great. Just as the given downvote is making a judgment call. I will not pretend that there are no judgments all the time in life. Judgments need to be for the right things, with discretion, with justice. I have no way to argue with a person who makes a judgment based on her taste. If person does not like what I wrote, I can not do anything. If she says that she voted because she has an error in her answer, then I can agree or debate and I can say that an answer without error does not deserve a negative answer.

  • 8

    In my view it is interesting to criticize codes, after all alerts the programmer which is the best option among the many existing. From the moment the code is exposed in a question or anywhere else, it is subject to judgments, in the same way that the answers, comments and opinions are also.

  • 2

    @Sérgiotorres in fact, especially when it comes to teaching other people. To program well or badly in principle is a problem of each one (and of who hired each one kkk), but when spreading knowledge on the site, becomes a problem of all of us at least indicate where there are problems.

  • Kkkkk, if I could, I would ask you to look at my code all made me do something to know if there is a way to do better or if I’m doing it the right way or wrong kkkkkk and the guy complaining because he said he should have used one while =/. I love to see someone telling the way and explaining why, I am more relaxed and confident writing the line of code kkkk

Show 5 more comments

Browser other questions tagged

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