Conditional statement help

So i'm having a bit of trouble with long conditional statements containing more than one operator. For example what would be the equivalent of the following conditional statement with parenthesis separating the statement into parts? (A >= B || B>=A && D==D || !I && F>G). Is it (((A >= B || B>=A) && D==D) || (!I && F>G))? What confuses me is the second set of ampersands since that is ahead of OR in the order of precedence. thx for the help in advance :)
I know this isn't really an answer, though if you have big complex conditional statements, it's generally better to have them in brackets...it stops you making stupid mistakes and makes it easier to read.
Can you explain what you want it to do better?

I will explain
(((A >= B || B>=A) && D==D) || (!I && F>G))
in english, and tell me if that is what you thought it should do.

If D is equal to D, (which it obviously always will be) AND A is not equal to B then condition is true.
OR (and this will only ever be checked is A equals B)
If I is false AND F is greater than G then condition is true.

I'm reasonably sure I right with that, and it's probably worth noting that that isn't a good way to right that, this is better:

((A!=B) ||(!I && F>G))
its ok. its a nice suggestion & i appreciate the input. The reason im asking is because i am currently taking a C++ course at my college & the professor wants us to at least be able to understand what is going on in a statement like this.
For extra long conditional statement, I don't know how others handle it. They may break it down further into many functions etc.

For me, I uses below approach but it may not be the most elegant one though. So you can treat it as a reference instead of a solution.

//declare an array of bool
bool result[3] = {false,false,false};

result[0] = A >= B;
result[1] = B>=A && D==D;
result[2] = I && F>G;

//then when use inside the if statement, the code is lesser and easier for understanding

if (result[0] || result[1] || !result[2]) { //look at this statement it is more concise and compact
..
}

@Painguy: Do you understand why it is the way it is though?
@TheMeerkat I understand what u did there. I have no problem simplifying conditional statements. My question is whether (A >= B || B>=A && D==D || !I && F>G) is equivelent to (((A >= B || B>=A) && D==D) || (!I && F>G)) (not the output, but rather the way/order the compiler reads/formats it)

@Sohguanh Thats a rather nice way of organizing it. Still doesn't answer the question, but at least i learned something new :P

btw thanks for trying to help me out guys.
Last edited on
Ah right, sorry I hadn't quite understood what you were asking at the start. Sorry I'm no help at all, I always just use loads of brackets all the time and have never bothered trying to learn this as a result. Even if you do know the way they compiler interrupts it, I think it is still better to place things in brackets just to make it much clearer for anyone else reading your code. sohguanh trick looks good if it's important to make it very clear.

You could have a look at this though http://www.cplusplus.com/doc/tutorial/operators/

there is a section called "Precedence of operators" that should be helpful.
Topic archived. No new replies allowed.