why doesnt this work

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main ()
{
	int x=0;
	int y=10;
	do
	{
		cout<<"5";
	}while((x>1)||(y>1));
}

it works if i put while(x>1);
or i change y=0;
You're never changing the values of x or y.
|| (OR, also known as "inclusive or") returns true is any of its operands are true.
The truth table for your condition is this:

x>1 y>1 return
F F F
F T T
T F T
T T T

1st iteration:
x=0, y=10
x>1? F
y>1? T
F || T? T (another iteration)

2nd iteration:
x=0, y=10 (no change)
x>1? F (no change)
y>1? T (no change)
F || T? T (another iteration)

3rd iteration:
x=0, y=10 (no change)
x>1? F (no change)
y>1? T (no change)
F || T? T (another iteration)

nth iteration
x=0, y=10 (no change)
x>1? F (no change)
y>1? T (no change)
F || T? T (another iteration)
Last edited on
Topic archived. No new replies allowed.