if ( initialize == "off" && Iminor > 20 || Imedium > 10 || Isevere > 5 || Ifatal > 0 )

the string type 'initialize' is cout'ing "on" and all of the values right of the AND operator are TRUE. Why is the subsequent code block for the if statement being executed? thanks!

Recommended Answers

All 3 Replies

This is a big OR statement. It says...

If any of the following four things are true, execute the statement:
1) initialize == "off" && Iminor > 20
2) Imedium > 10
3) Isevere > 5
4) Ifatal > 0

You say that 2, 3 and 4 all come out as true? Well then, it seems clear that the statement will be executed.

Did you mean this:

if ( (initialize == "off") && (Iminor > 20 || Imedium > 10 || Isevere > 5 || Ifatal > 0))
which says execute the statement if initialize == "off" and any of the following things are true:
Iminor > 20
Imedium > 10
Isevere > 5
Ifatal > 0

&& has higher precedence than ||, so the evaluation looks like this:

if ( (initialize == "off" && Iminor > 20) || Imedium > 10 || Isevere > 5 || Ifatal > 0 )

When what you really want is this:

if ( initialize == "off" && (Iminor > 20 || Imedium > 10 || Isevere > 5 || Ifatal > 0) )

thankyou very much gentlemen, it worries me that i never knew this ^^

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.