&& and || works different

when i put "||" program end when x and y = 0, if i put "&&" program end when x or y is 0.
can someone explain?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <iostream>
using namespace std;

double funk(int x, int y);

int _tmain(int argc, _TCHAR* argv[])
{
	int x, y;
	cout << "2 numbers: ";
	cin >> x;
	cin >> y;
	while (x!=0 || y!=0) // problem
	{
		cout << funk (x ,y) << endl;
		cin >> x >> y;
	}
	cout << "end \a" << endl;

	return 0;
}
double funk (int x, int y)
{
	return 2.0*x*y/(x+y);
}
http://www.cplusplus.com/doc/tutorial/operators/

Skip down to the logical operators section.
and what we see ? "||" is or and "&&" is and but in my program "||" work like and. thats why i am posting.
read the question correct
while(true){}

Evaluate:

&& OPERATOR
a b (a && b)
true true true
true false false
false true false
false false false


|| OPERATOR
a b (a || b)
true true true
true false true
false true true
false false false
Last edited on
while (x!=0 || y!=0) // problem

While loops keep looping as long as the condition is true.
This condition will be true as long as:

x does not equal 0
OR
y does not equal 0

So as long as either one of them is nonzero, the loop will continue.
So the only way for the loop to end would be for both x and y to be zero.
and what we see ? "||" is or and "&&" is and but in my program "||" work like and. thats why i am posting. read the question correct.


Love the attitude. I did read the question correctly. You don't understand how the || and && operators work. If you read the explanation in the link provided and plugged the appropriate values into the tables shown there you would see that your program works exactly according to the proscribed behavior of the || and && operators. But I suppose skimming over it and using your impression of how it should work is "reading the explanation correct?"
Topic archived. No new replies allowed.