c++ - boolean variables for loops vs conditions -


here code:

int main() { int input; bool iszero = false;  while (!iszero) {      ...      if (0 == input)     {         iszero = true;     }      else     {         iszero = false;     }  }  return 0; } 

the program supposed to, feel !iszero expression not foolproof.

is

bool iszero = false; while(!iszero); {    .... } 

the same thing as

bool iszero = false; while(iszero == false) {      ... } 

why or why not? also, in cases true represent 1, , in cases represent nonzero number?

while requires condition boolean , !iszero boolean. equality operator generates boolean, again iszero == false boolean. thing changed adding operator , (possibly) slowing down loop little (afair comparison slower bit operators).

operations on boolean values similar operations on integer values. concerns can translated integers: "hey, x == 1 not foolproof, have check, whether x - 1 == 0".

the bottomline is, !iszero absolutely foolproof enough.

as int bool conversions, standard says,

a prvalue of arithmetic, unscoped enumeration, pointer, or pointer member type can converted prvalue of type bool. 0 value, null pointer value, or null member pointer value converted false; other value converted true. direct-initialization (8.5), prvalue of type std::nullptr_t can converted prvalue of type bool; resulting value false.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -