Can I do this?

while(variable=functionCall()!=0){

     /* ... */

}

the function I'm calling, is returning a 3, but the variable in the while loop is equal to 1...
I'm not sure what is happening in this statement, Originally I thought it would just always evaluate to true and loop forever since, variable would be successfully set to functionCall()'s return value, therefore evaluating to 1, and 1 does not equal zero, so that too would evaluate to 1 (true) and loop forever... but it sets variable to 1 and then exits the while loop--what's going on?

functionCall() gets input from the user so I can only call it once really, I was going for while(functionCall()!=0){ } , but then I wouldn't be able to check the return value within the loop. I could do something else but I wanted to use this method, so I'm just wondering what it's actually doing.

Dani AI

Generated

Short answer: the comparison runs before the assignment, so the variable receives a boolean (true/false) converted to 1/0, not the raw numeric return from your input function.

As pointed out, the comparison yields true when the function return is nonzero, and that true becomes 1 when assigned to an integer. The underlying reason is operator precedence: relational operators are evaluated before =. (Assignment itself also yields the assigned value, which is why the while still tests a 1/0 result.)

was right to suggest forcing the assignment to happen first; adding explicit grouping makes the intent clear and avoids accidental boolean assignment. A clearer and less error-prone pattern is to assign once before the loop and update inside it, for example:

int value = getInput();
while (value) {
    // use value here
    value = getInput();  // get next input
}

Troubleshooting tips:

  • Enable compiler warnings (e.g. -Wall -Wextra for GCC/Clang). They often flag suspicious assignments in conditions.
  • Print or inspect the function return and the variable type to confirm what value is stored.
  • Consider using bool if you only care about truthiness, or a named integer if you need the numeric result.
  • If the loop logic is confusing, rewrite it with an explicit assignment and a clear comparison or use a do/while pattern so the control flow is obvious.

This explains why you saw 1 in the variable and how to write the loop so you actually capture the function's numeric return for use inside the loop.

Recommended Answers

All 4 Replies

variable would be successfully set to functionCall()'s return value

no way, variable is set to true, because functionCall()!=0 is true. in this case true means 1

p.s. And yes, this does loop forever if functionCall() really returns 3

you should try something like this:

while ((variable = functionCall())!=0)

...
because it seems to me that variable is given the value of evaluation: functionCall()!=0, so you just need to use one more pair of brackets...
I hope this helps :D

@da penguin, oh right, I forgot about right->left order, and you're right it was looping forever--there are two loops working together and I was mistaken with which loop I was in at what time.

, it worked great, thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.