c++ – Difference in parentheses while with return


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 return is verifying only the first condition …


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

while ( expresion ) {}

return expresion;

The parentheses can be used to stand or group expressions of any type (arithmetic, conditionals, etc.) in the same statement, 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 between parentheses.

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

Scroll to Top