Question:
In C++, Is there a way to write an if statement to check multiple conditions before running?
metalgearsolid
2011-02-03 19:18:39 UTC
For instance: I'm trying to run this loop, but only if modulo of 'a' 'b' and 'c' == to 0. I tried doing it like this:

if (a%2 == 0, b%2 == 0, c%2 == 0)
(run loop)

Unfortunately this did not work. If I only post one condition like this

if (a%2 == 0)

then it works good. How do I make it so one if statment will check all 3 conditions before running a loop?
Four answers:
Silent
2011-02-03 19:23:30 UTC
Of course. You should probably read up on boolean operators in C++, but this is what you're basically looking for:



if(a%2 == 0 && b%2 == 0 && c%2 == 0)



&& is the boolean AND operator. It returns true if both of the operands are true.



|| is the boolean OR operator. It returns true if at least one of the operands is true (one, or the other, or both).



^ is the XOR operator. It returns true if exactly one of the operands is true (one, or the other, but not both).



! is the NOT operator. It returns true if its single operand is false, and false if its operand is true.
fondrisi
2016-11-29 12:41:28 UTC
OMG you're literally the best individual I even have ever met (or examine from). All of those books are dazzling and that i basically won't be able to see The Witches and Animal Farm are banned. they might to boot permit you recognize the only element you are able to watch is the information on television and nevertheless block the comments pertaining to to the middle East. you have graduated by now i assume so did you get away with it for something of your extreme college profession? you're are basically freaking stunning, I choose you have been right here spectacular now so i might desire to purchase you a machiatto or something from Starbucks. You bypass woman, like that is incredible. that is honestly you are able to tell human beings as a narrative or something. you're freaking dazzling. I salute you and wager you're off now doing something dazzling including your life. Congratulations! you're an concept to countless little ones. Like despite in case you get carry of like suspended or something (in spite of the undeniable fact that that is not solid in any respect) a minimum of it would have been something cool and incredible. basically... you bypass woman,:)
v491138
2011-02-03 19:27:51 UTC
I know I'm in over my head on this and this answer is likely only a hint.

"case of' type should allow creation of the logic you desire. It might be a kluge, but it should get you there. It has been MANY years since I claimed to be a programmer, but I think you might find this of use.
The Phlebob
2011-02-03 19:30:50 UTC
Use &&s instead of commas to get a compound conditional statement.



Hope that helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...