Hi guys, I am not sure this is right or not.
If I have one variable whose value is true or false and I want to use it in an if statement I could easily do

if(variable1){
    //do this and that
}

but I want to check 2 variables I assume I will have to explicitly test whether they are true or false like so

if((variable1 == true) && (variable2 == true)){
    //do this and that
}

I can't have a situation like

if((variable1) && (variable2)){
    //do this and that
}

correct?

No, it's perfectly fine to write if(variable1 && variable2). What makes you think that doesn't work?

The if statement in the last case will only be executed if both var1 and var 2 are true.

ah ok, I don't know I just thought this was incorrect, sorry!

if((variable1) && (variable2)){
    //do this and that
}

So if the above is fine then even this would be ok:

if((variable1) && !(variable2)){
    //do this and that
}

I just thought that if there is more than a variable involved I had to explicitly test the variables

== is an operator that takes two operands and produces the value true if the operands are equal and false if they're not. The boolean values produced by == aren't any different than the boolean values stored in your variables. There is no context in which using == would be okay, but using a boolean variable would not be (assuming there isn't some stupid operator== overload in play that returns something other than a boolean). A boolean expression is a boolean expression.

Same goes for !: you can use it anywhere you can use any other boolean expression.

^ like he said, plus you don't need to put the variable names in parenthesis
if (variable1 && ! variable2) ...

ok thanks guys!

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.