Steven Nguyen
Steven Nguyen

Reputation: 11

Question about IF statement's condition and how to make it work

So I was working on an IF statement where

if (((number_one > 10) + (number_two < -10)) == 5) {
    printf("%d + %d = five", number_one, number_two);

I basically wanted the code to recognise that if the user inputted the first number (number_one) and it met the condition that it was greater than 10, and the second number (number_two) was less than -10, then when both numbers are added together and equals to 5, the if condition is fulfilled and the printf statement will happen. The issue is, this doesn't work due to "== 5" as when I remove the "== 5", the condition works with my program and when it's there, it doesn't. Can anyone provide suggestions or workarounds? I'm a beginner and I only know of printf, scanf, if/else if/else statements so using those would be preferred. Thank you!

Upvotes: 1

Views: 170

Answers (2)

RoQuOTriX
RoQuOTriX

Reputation: 3001

Let's brake this line which you wrote here (we are going from in to out):

(((number_one > 10) + (number_two < -10)) == 5)

  (number_one > 10) => this evaluates to true or false, which means true == 1 and false == 0

                      (number_two < -10) => the same as the first

So you are either adding

0+0 
0+1 
1+0 
1+1

which will never evaluate to 5

Look at @Jarod42's answer how to implement the conditions correctly

Upvotes: 2

Jarod42
Jarod42

Reputation: 217085

Use && or and:

if ((number_one > 10) && (number_two < -10) && (number_one + number_two  == 5)) {
  /*..*/
}

Upvotes: 1

Related Questions