Difference in parentheses while with return

7

Why can I do this:

return (tarea->ini == NULL) && (tarea->fin == NULL); // no hay parentesis global

but not

while (tarea->ini != NULL) && (tarea->ini != kol) // no hay parentesis global

?

since it tells me that I should put the while like this:

while ((tarea->ini != NULL) && (tarea->ini != kol)) // parentesis global

I want to know why it is not going to be something that that return is verifying only the first condition ...

    
asked by Edu 20.03.2018 в 04:12
source

3 answers

9

The short answer is: because while expects an expression in parentheses and the return does not.

while ( expresion ) {}

return expresion;

Parentheses can be used to group or group expressions of any type (arithmetic, conditional, etc.) in the same sentence, as in the case of the return statement.

On the other hand, while , like the other control structures (if, for, switch, etc.) requires that one or more conditional expressions be specified in parentheses.

In short, the "global" parentheses that you use in a while statement do not mean the same as the ones you are using in the return statement.

    
answered by 20.03.2018 / 17:26
source
8

It is not that there is a revealing reason in itself (at least as far as I know), it is simply that the syntax of both is defined in that way.

The while is a conditional structure that requires a condition in parentheses to work:

while (condición)
{
    // Cosas
}

The return is a statement that does not contain parentheses in its syntax:

return valor;

What it does require is a character to separate the reserved word return from what is coming, it does not necessarily have to be separated by a space, really any character that can not be used in an identifier because then when "return value" is joined compiler would take the whole word as an identifier.

Then the valid characters to separate return from what is coming are all those that are not letters, numbers or low guinlines, for example:

return+10; // El separador es el signo +
return(10); // El separador es el signo (
return 10;  // El separador es el signo espacio
return{10}; // El separador es el signo {
return10;  // NO hay separador y "return10" se toma como un solo elemento, el compilador probablemento no encontrará dicho elemento y dará un error

In any of the previous cases, it is valid to say that the entire expression is evaluated (It is saying, omitting the fact that there are operations in short circuit) so there is no difference between using parentheses or not.

    
answered by 20.03.2018 в 05:57
4

In the case of the return what happens is that it is doing an operation and in the end it can be true or false

return (tarea->ini == NULL) && (tarea->fin == NULL); // no hay parentesis global

In this other case of while the syntax asks you to be

while (condicion){
//codigo
}

If you put it in the following way you do not know which is the operation you are trying to evaluate, that is why it must go inside the parentheses.

while (tarea->ini != NULL) && (tarea->ini != kol)
    
answered by 20.03.2018 в 07:28