Problems with || and &&

Hello there.

I am new to c++ but have been coding pawn/small for a while.

I am trying to just make a simple calculator which divides, but I can't get the check working to see if you are dividing with 0.

I have tried various things and the thing I came up with is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
int main()
{
   int x, y;
   std::cout << "Tal 1: ";
   std::cin >> x;
   std::cout << "Tal 2: ";
   std::cin >> y;
// != betyder ikke er... Hvis du har == betyder er..
   if(y != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   }
   else if (x != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   }
   else
   {
       std::cout << "Kan ikke dividere med 0" << std::endl;
   }
}


I also tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
int main()
{
   int x, y;
   std::cout << "Tal 1: ";
   std::cin >> x;
   std::cout << "Tal 2: ";
   std::cin >> y;
// != betyder ikke er... Hvis du har == betyder er..
   if(y != 0 || x != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   }
   else
   {
       std::cout << "Kan ikke dividere med 0" << std::endl;
   }
}


But it still lets me divide by 0. I have always had trouble with these things but now I thought it would be about time to find out how it actually works.

Thanks!
It does not matter if x is equal to 0 or not. The reason your checks are failing is because if x is not 0 it will run even if y is equal to 0 seeing as you are using different if statements or using ||.

1
2
3
4
5
6
7
8
9
  if(y != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   }

   else
   {
       std::cout << "Kan ikke dividere med 0" << std::endl;
   }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int x, y;
   std::cout << "Tal 1: ";
   std::cin >> x;
   std::cout << "Tal 2: ";
   std::cin >> y;
// != betyder ikke er... Hvis du har == betyder er.. //don't know what this comment means in English and I couldn't be bothered to go to Google translate
   if(y != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   }
   
   else
   {
       std::cout << "Kan ikke dividere med 0" << std::endl;
   }
Last edited on
1
2
3
4
   if(y != 0 || x != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   }


This will not work the way you intended (Probably the reason you're here lol)

Well, you need BOTH y and x to be non-zero. In the way you wrote, you are requesting if only Y or only X is non-zero.
Results of the Tests:

x = 1
y = 1
1/1=1

x = 0
y = 0
kan ikke dividere med 0

x = 1
y = 0
1/0 = (error, exception or some number extremely weird)

x = 0
y = 1
0/1 = 0


What you need to use is the unary And operator (&&):
1
2
3
4
   if(y != 0 && x != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   } else { ...


Also, 0 is divisible by any number. You can still hold that functionality and do:
1
2
3
4
   if(y != 0)
   {
       std::cout << x << " / " << y << " = " << x / y << std::endl;
   } else { ...
Last edited on
It's division by zero that's not so good. You can divide 0 by another number just fine.

so:

1
2
3
4
5
6
if ( y == 0 )
{
    // division by 0.
}
else
    // do division 

However, if for some reason you actually didn't want to do the division if either of the two were 0:

1
2
3
4
5
6
if ( y != 0 &&  x != 0 )
{
    // do division
}
else
    // division with 0 


alternately:

1
2
3
4
5
6
if ( y == 0  ||  x == 0 )
{
    //division with 0
}
else
    // do division 

Last edited on
What you need to use is the unary And operator (&&):


I'm sure it was just a mistype, but the logical and operator (&&) is binary.
Yes it worked. Thx so much..

I still think it's kinda wierd...

|| means = or
&& means = and

I think it's wierd that i have to say: if x and y is 0 then no division..

Then both have to be 0 before it doesn't devide? What if I want to to say 0/7? Then x and y isn't both 0?

That's why for me it's much more logical to use || (or), because then it doesn't devides if just 1 of them is 0..

Thanks so much anyway! I just think like this... For me it's opposite of my logical sense..
Last edited on
I think it's wierd that i have to say: if x and y is 0 then no division..


On the contrary, if you want no division to occur when either of x or y is 0, you must not divide if x or y is 0.

You use && when you want to evaluate whether "condition a is true and condition b is true". You use || when you want to evaluate whether "condition a is true or condition b is true."

That's why for me it's much more logical to use || (or), because then it doesn't devides if just 1 of them is 0..

You used or with inequality which, not surprisingly, leads to different results than using or with equality.


The original code you supplied (well, at least the second snippet) looks like:

1
2
3
4
if(y != 0 || x != 0)
    // do division
else
    // don't do division  


In English: If y is not 0 or if x is not 0, do division. The problem there of course is that x can be not 0 while y is 0, and the condition evaluates to true, because that's what we asked for.

Consider what the if statements look like for particular values of x and y when the expressions are replaced with the true or false value they evaluate to:

 x = 0, y = 0
if ( y!=0 || x!=0 )    ---->    if ( false || false )    ---->    false

x=1, y=0
if ( y!=0 || x!=0 )    ---->    if ( false || true )     ---->    true

x=0, y=1
if ( y!=0 || x!=0 )    ---->   if ( true || false )    ---->    true

x=1, y=1
if ( y!=0 || x!=0 )    ---->    if ( true || true )    ---->    true


Last edited on
It makes way more sense now, after reading your table about true and false.. Thank you.. Hopefully i'll be "wired in" the next couple of hours before the next problem appears.. :)
Topic archived. No new replies allowed.