using an or statement for multiple items?

How do I say if grade equals 69 or 79 or 89 etc. I tried using the or symbol but that didnt work (it made the sign a + for every input no matter what). Is there away to do this other than writing out :
if (grade == 69)||(grade==79||(grade==89) etc. etc.
?



here is my actual code:
1
2
3
4
5
if (grade == 69,79,89,99,68,78,88,99,100) 
		{
			sign ='+';
		}
			
Duplicate of http://cplusplus.com/forum/beginner/64027/

I gave your answer there. Here it is again.
1
2
if (grade%10 == 9)
  sign = '+';

You'll need to use a special case for the 100 case:
1
2
if (((grade%10 == 9) && (grade > 60)) || (grade == 100))
  sign = '+';
Last edited on
Thanks for the answer. I didn't see that on the other thread. But this isnt a duplicate, I decided to do it a different way by listing them all out where as before I was trying to make a .at statement do it for me. Thanks.

Also if I did want to list a bunch of numbers like I stated above how would I do it?
Would I have to write them all out like if (grade == 69)||(grade==79||(grade==89) etc. etc.?
Last edited on
I did it the way you explained and it works great. I guess what I am trying to ask is why doesnt this work?

1
2
3
4
if (score == 69||79||89||99||68||78||88||99||100) 
		{
			sign ='+';
		}
I'm assuming that you are familiar with order of operations in math. (i.e. do () first, then ^, then */, then +-).

There is also an order of operations for C++. See: http://en.cppreference.com/w/cpp/language/operator_precedence

The compiler is first looking at the ==, then it looks at the ||. So things reduce like this:

1
2
3
4
5
6
7
8
9
10
(score == 69) || 79 || 89 || 99 || 68 || 78 || 88 || 99 || 100
(false|| 79) || 89 || 99 || 68 || 78 || 88 || 99 || 100
(true || 89) || 99 || 68 || 78 || 88 || 99 || 100
(true || 99) || 68 || 78 || 88 || 99 || 100
(true || 68) || 78 || 88 || 99 || 100
(true || 78) || 88 || 99 || 100
(true || 88) || 99 || 100
(true || 99) || 100
(true || 100)
true


The function will always return true.

Here's something that might work for you:
1
2
3
4
5
6
7
const int plusses[] = {69,79,89,99,68,78,88,98,100};
bool is_plus = false;
for (int i = 0; i < 9; i++)
  if (score == plusses[i]) 
    is_plus = true;
if (is_plus)
  sign = '+';


But that's a bit more complicated than just:
if (score == 69 || score == 79 || score == 89 || ...)
Last edited on
I understand order of operations I just don't understand why an input for score of say 85 would return true. I understand why it returns true for those values(69,79,89etc.), but I don't understand why it returns true for every value of score.

Sorry if I am kind new to this stuff. You have been very patient with me and I appreciate your help.
@Algar32: (Nice name. I prefer Riva.)

Basically, each side of a logical operator is a complete statement. So if you say:

if(score == 69||79)
Then you're saying 'if score equals 69 or if 79 exists'.

However, if you were to say:

if(score == 69 || score == 79)
Then you're putting a complete statement on both sides of the operator, and it will read as 'if score equals 69 or if score equals 79'.
Also remember: true is defined as not zero. So we could also put it like this:
if(score == 69 || 79)
and score is 85 so
if(85 == 69 || 79)
we do the == first (order of operations)
if(false || 79)
and true is anything that is not zero, so 79 is true:
if(false || true)
false || true is true
if(true)

Tada
So basically anything I line of in those or statements that isnt a zero will get marked as true. So the whole statement will be true as long as one item in the or statement isn't 0.
Thanks guys this helped!
Well, anything that doesn't equal 0 OR evaluate to false (0).
Topic archived. No new replies allowed.