Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0
Answers
Answered by
12
A=(x/x+1greater than)
(x/x+1 less than 0)
(x/x+1 less than 0)
Answered by
0
Answer:
b) if (x > 0) x++;
else if (x < 0) x--;
Step-by-step explanation:
If x is positive (x>0) then x++ is performed and if x is negative (x <0) then x-- is performed and in any other case x will remain unchanged.
Your Question is incomplete so Let me complete the question for you .The statements to be provided as option are :
a) if (x > 0) x++;
else x--;
b) if (x > 0) x++;
else if (x < 0) x--;
c) if (x > 0) x++;
if (x < 0) x--;
else x = 0;
d) if (x == 0) x = 0;
else x++;
x--;
e) x++;
x--;
In all the other options the logic is wrong
- In a) option, x-- is done when x is not positive so when x=0 it becomes -1
- In c) if x is positive ,x++ is done then in that case, the next statement is executed and if x is not negative, the else clause is performed setting x to 0. So if x is positive, it becomes 0 after this set of code.
- In d), x++ and x-- are both executed if x is not 0.
- In e), this code does not try to determine if x is positive or negative, it just adds one and then subtracts 1 from x leaving x as it was.
Similar questions