We appreciate your visit to What will be the values of ans x and y after the following statements are executed cpp int ans 35 x 50 y 50 if. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!

What will be the values of `ans`, `x`, and `y` after the following statements are executed?

```cpp
int ans = 35, x = 50, y = 50;
if (x >= y) {
ans = x + 10;
x -= y;
} else {
ans = y + 10;
y += x;
}
```

A. ans = 60, x = 50, y = 100
B. ans = 60, x = 0, y = 50
C. ans = 45, x = 50, y = 0
D. ans = 45, x = 50, y = 50

Answer :

Answer:

B. ans = 60, x = 0, y = 50

Step wise Explanation of the code chunk

  • The first statement assigns value 35 to ans variable, value of 50 to x variable and value of 50 to y variable.
  • Next is an if statement which checks if the value of x variable is greater than or equal to y
  • This condition is true as x = 50 and y = 50 So x = y
  • Now since the statement is true it will enter the if block of the code which is the instruction: ans = x + 10; x -=y; and ignores the else block.
  • This line changes the value of ans variable and assigns it the value obtained by adding 10 to the value of variable x.
  • we know that x = 50

So

ans = x + 10

ans = 50 + 10

ans = 60

  • After this, comes another line x -=y; which means decrement the value of x by value of y

that means:

x = x - y

x = 50-50

x = 0

  • So the new value of ans is 60, new value of x is 0 and the value of y remains the same i.e. 50

Hence

ans = 60

x = 0

y = 50

Thanks for taking the time to read What will be the values of ans x and y after the following statements are executed cpp int ans 35 x 50 y 50 if. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!

Rewritten by : Barada