Hi everyboy,

I have a question how to add not boolean expression to the conditional loop,

Imagine some situation where I have :

while(some expression && another expression) { .... }

So lets say I want to calculate how many times "another expression" was evaluated. I wish I would stick something like count++ before it but it doesn't work this way.
I can't put count++ at the begining of while loop because it wouldn't be incremented if "another expression" is false.

If you have any idea please help me =)

6 Years
Discussion Span
Last Post by shean1488

Can you use an if(!myExpression) statement?

while(some_expression && another_expression)

Thines01: that still won't increment when the initial condition is false as it will not enter the loop.

shean1488: actually you can do exactly as you wished, simply add count++ to your condition statement...

while(count++ > 0 && some expression && another expression)

this will increment count each time the condition is evaluated and (so long as count is not negative) will always evaluate to true and thus not affect the outcome of the condition statement.


yet will not do what shean asked for, since it will augment if either one of them is true, it will even augment if none of them are true.
just take Thines' code and alter it a bit:

int expression2Evaluated = 0;
while ( expression1 && expression2 ){
  expression2Evaluated += 1;
  //rest of the code
// now, expression2 in the last time may have caused the error, but it would have been evaluated IF expression1 was true, so:
if ( expression1 )
  expression2Evaluated += 1;

or, on your code, which would be easier (typing this during lunch so yah, can make stupid mistakes too :p )

while ( expression1 && expression2Evaluated++ > -1 && expression2 )

Edited by stultuske: made it more difficult than needed, added simpler code

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.