I have to write the output without using a computer. What is the output of the f
ID: 3863827 • Letter: I
Question
I have to write the output without using a computer.
What is the output of the following code?
int x = 9;
while (x > 0) {
if( x > 6 || x % 5)
cout << "x == " << x << endl;
x -= 2;
}
cout << "x == " << x << endl;
I know that the answer is:
x == 9
x == 7
x == 3
x == 1
x == -1
I ran it and that is the output.
Can you explain why x == 5 was not on the output? Maybe it has something to do with x % 5.
I know that the modulus sign gives the remainder! But in this case it just say x % 5 and the examples that i saw it always had something like x % 5 == 0.
In this case it is not comparing it to anything. Please explain in details so i can learn and see what i am doing wrong.
Explanation / Answer
if condition will only execute when the conditions are true ,here we have if( x > 6 || x % 5) i.e either x should be greater than 6 or x % 5 must not give 0.
in case of x=5 both conditions are false hence if statement has not been executed resulting you would not able to see x==5.
Note:Please do ask in case of any doubt,thanks.