Hi I have these two separate if statements, when put like so ;
if (powerlevel <= 0) // <--- ends up having no effect
if (src.health <= 0)
    the_thing_to_do();
How do I combine these two if statements into one? is it possible? If so how?
Hi I have these two separate if statements, when put like so ;
if (powerlevel <= 0) // <--- ends up having no effect
if (src.health <= 0)
    the_thing_to_do();
How do I combine these two if statements into one? is it possible? If so how?
 
    
     
    
    If you want both statements to be true use logical AND
if(powerlevel <= 0 && src.health <= 0) 
If you want either of the statements to be true use logical OR
if(powerlevel <= 0 || src.health <= 0) 
Both of the above operators are logical operators
 
    
    Use operator&& if you want both of them to be met (logical AND)
if(powerlevel <= 0 && src.health <= 0) { .. }
or operator|| if you want just one to be met (logical OR)
if(powerlevel <= 0 || src.health <= 0) { .. }
 
    
    It depends if you want both to evaluate to true...
if ((powerlevel <= 0) && (src.health <= 0)) {
  // do stuff
}
... or at least one ...
if ((powerlevel <= 0) || (src.health <= 0)) {
  // do stuff
}
The difference being logical AND (&&) or logical OR (||)
 
    
     
    
    Or if you don't want to use && you can use a Ternary Operator
#include <iostream>
int main (int argc, char* argv[])
{
  struct
  {
      int health;
  } src;
  int powerlevel = 1;
  src.health = 1;
 bool result((powerlevel <= 0) ? ((src.health <=0) ? true : false)  : false);
 std::cout << "Result: " << result << std::endl;
}
 
    
     
    
    Just an aternative if it is meaningful(sometimes).
Both true:
if (!(src.health > 0  || powerlevel > 0)) {}
at least one is true:
if (!(src.health > 0  && powerlevel > 0)) {}
