Suppose a variable x can have only two possible values a and b, and you wish to assign to x the value other than its current one. Do it efficiently without using any conditional operator.

Note : We are not allowed to check current value of x.

Examples:

Input : a = 10, b = 15, x = a

Output : x = 15

Explanation x = 10, currently x has value of a (which is 10), we need to change it to 15.Input : a = 9, b = 11, x = b

Output : x = 9

We could solve this problem using if condition, but we are not allowed to do that.

if (x == a) x = b; else x = a;

We could have used Ternary operator, it also checks the current value of x and based on that it assigns new value. So we cannot use this approach as well

x = x == a ? b : a;

But, we are not allowed to check value of x, so none of the above solutions work.

**Solution 1:** Using arithmetic operators only we can perform this operation

x = a + b - x

This way the content of x will alternate between a and b every time it gets executed

## C++

`// CPP program to change value of x ` `// according to its current value. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to alternate the values ` `void` `alternate(` `int` `& a, ` `int` `& b, ` `int` `& x) ` `{ ` ` ` `x = a + b - x; ` `} ` ` ` `// Main function ` `int` `main() ` `{ ` ` ` `int` `a = -10; ` ` ` `int` `b = 15; ` ` ` `int` `x = a; ` ` ` `cout << ` `"x is : "` `<< x; ` ` ` ` ` `alternate(a, b, x); ` ` ` ` ` `cout << ` `"\nAfter change "` `; ` ` ` `cout << ` `"\nx is : "` `<< x; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to change value of x ` `// according to its current value. ` `import` `java.util.*; ` ` ` `class` `solution ` `{ ` ` ` `// Function to alternate the values ` `static` `void` `alternate(` `int` `a, ` `int` `b, ` `int` `x) ` `{ ` ` ` `x = a + b - x; ` ` ` `System.out.println(` `"After change"` `+` `"\n"` `+` `" x is : "` `+x); ` `} ` ` ` `// Main function ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `a = -` `10` `; ` ` ` `int` `b = ` `15` `; ` ` ` `int` `x = a; ` ` ` `System.out.println(` `"x is : "` `+x); ` ` ` `alternate(a, b, x); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to change value ` `# of x according to its current value. ` ` ` `# Function to alternate the values ` `def` `alternate(a,b,x): ` ` ` `x ` `=` `a` `+` `b` `-` `x ` ` ` `print` `(` `"After change x is:"` `,x) ` ` ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `-` `10` ` ` `b ` `=` `15` ` ` `x ` `=` `a ` ` ` `print` `(` `"x is:"` `,x) ` ` ` `alternate(a,b,x) ` ` ` `# This code is contributed by ` `# Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to change value of x ` `// according to its current value. ` ` ` `using` `System; ` `class` `gfg ` `{ ` ` ` `// Function to alternate the values ` ` ` `public` `void` `alternate(` `ref` `int` `a, ` `ref` `int` `b, ` `ref` `int` `x) ` ` ` `//'ref' indicates the references ` ` ` `{ ` ` ` `x = a + b - x; ` ` ` `} ` `} ` ` ` `// Main function ` `class` `geek ` `{ ` ` ` `public` `static` `int` `Main() ` ` ` `{ ` ` ` `gfg g = ` `new` `gfg(); ` ` ` `int` `a = -10; ` ` ` `int` `b = 15; ` ` ` `int` `x = a; ` ` ` `Console.WriteLine(` `"x is : {0}"` `, x); ` ` ` ` ` `g.alternate(` `ref` `a, ` `ref` `b, ` `ref` `x); ` ` ` ` ` `Console.WriteLine (` `"After change "` `); ` ` ` `Console.WriteLine(` `"x is : {0}"` `, x); ` ` ` `return` `0; ` ` ` `} ` `} ` `//This code is contributed by Soumik ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to change value of x ` `// according to its current value. ` ` ` `// Function to alternate the values ` `function` `alternate (&` `$a` `, &` `$b` `, &` `$x` `) ` `{ ` ` ` `$x` `= ` `$a` `+ ` `$b` `- ` `$x` `; ` `} ` ` ` `// Driver Code ` `$a` `= -10; ` `$b` `= 15; ` `$x` `= ` `$a` `; ` `echo` `"x is : "` `, ` `$x` `; ` ` ` `alternate(` `$a` `, ` `$b` `, ` `$x` `); ` ` ` `echo` `"\nAfter change "` `; ` `echo` `"\nx is : "` `, ` `$x` `; ` ` ` `// This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

x is : -10 After change x is : 15

**Time Complexity:** The time complexity of this approach is O(1)

**Space Complexity:** The space complexity of this approach is O(1)

**Solution 2:** A better and efficient approach is using the bitwise XOR operation.

x = a^b^x

## C++

`// CPP program to change value of x ` `// according to its current value. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to alternate the values ` `void` `alternate(` `int` `& a, ` `int` `& b, ` `int` `& x) ` `{ ` ` ` `x = a ^ b ^ x; ` `} ` ` ` `// Main function ` `int` `main() ` `{ ` ` ` `int` `a = -10; ` ` ` `int` `b = 15; ` ` ` `int` `x = a; ` ` ` `cout << ` `"x is : "` `<< x; ` ` ` ` ` `alternate(a, b, x); ` ` ` ` ` `cout << ` `"\nAfter exchange "` `; ` ` ` `cout << ` `"\nx is : "` `<< x; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to change value of x ` `// according to its current value. ` ` ` `class` `GFG { ` `// Function to alternate the values ` ` ` ` ` `static` `int` `alternate(` `int` `a, ` `int` `b, ` `int` `x) { ` ` ` `return` `x = a ^ b ^ x; ` ` ` `} ` ` ` `// Main function ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `a = -` `10` `; ` ` ` `int` `b = ` `15` `; ` ` ` `int` `x = a; ` ` ` `System.out.print(` `"x is : "` `+ x); ` ` ` ` ` `x = alternate(a, b, x); ` ` ` ` ` `System.out.print(` `"\nAfter exchange "` `); ` ` ` `System.out.print(` `"\nx is : "` `+ x); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to change value of x ` `# according to its current value. ` ` ` `# Function to alternate the values ` `def` `alternate(a, b, x): ` ` ` `x ` `=` `a ^ b ^ x ` ` ` `print` `(` `"After exchange"` `) ` ` ` `print` `(` `"x is"` `, x) ` ` ` `# Driver code ` `a ` `=` `-` `10` `b ` `=` `15` `x ` `=` `a ` `print` `(` `"x is"` `, x) ` `alternate(a, b, x) ` ` ` `# This code is contributed ` `# by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# program to change value of x ` `// according to its current value. ` `using` `System; ` `public` `class` `GFG { ` `// Function to alternate the values ` ` ` ` ` `static` `int` `alternate(` `int` `a, ` `int` `b, ` `int` `x) { ` ` ` `return` `x = a ^ b ^ x; ` ` ` `} ` ` ` `// Main function ` ` ` `public` `static` `void` `Main() { ` ` ` `int` `a = -10; ` ` ` `int` `b = 15; ` ` ` `int` `x = a; ` ` ` `Console.Write(` `"x is : "` `+ x); ` ` ` ` ` `x = alternate(a, b, x); ` ` ` ` ` `Console.Write(` `"\nAfter exchange "` `); ` ` ` `Console.Write(` `"\nx is : "` `+ x); ` ` ` ` ` `} ` `} ` `/*This code is contributed by Rajput-Ji*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to change value of x ` `// according to its current value. ` ` ` `// Function to alternate the values ` `function` `alternate(&` `$a` `, &` `$b` `, &` `$x` `) ` `{ ` ` ` `$x` `= ` `$a` `^ ` `$b` `^ ` `$x` `; ` `} ` ` ` `// Driver Code ` `$a` `= -10; ` `$b` `= 15; ` `$x` `= ` `$a` `; ` `echo` `"x is : "` `, ` `$x` `; ` ` ` `alternate(` `$a` `, ` `$b` `, ` `$x` `); ` ` ` `echo` `"\nAfter exchange "` `; ` `echo` `"\nx is : "` `, ` `$x` `; ` ` ` `// This code is contributed ` `// by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

x is : -10 After exchange x is : 15

**Time Complexity:** The time complexity of this approach is O(1)

**Space Complexity:** The space complexity of this approach is O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Internal static variable vs. External static variable with Examples in C
- Programming puzzle (Assign value without any control statement)
- Mapping external values to dataframe values in Pandas
- 5 variable K-Map in Digital Logic
- Swapping four variables without temporary variable
- __file__ (A Special variable) in Python
- Why use Question mark in TypeScript variable ?
- How to swap two numbers without using a temporary variable?
- Swap three variables without using temporary variable
- Count occurrences of a sub-string with one variable character
- Multiplying a variable with a constant without using multiplication operator
- How to call PHP function from string stored in a Variable
- Python Program to swap two numbers without using third variable
- What is the difference between field, variable, attribute, and property in Java
- How to wait for a promise to finish before returning the variable of a function?
- Number of values of b such that a = b + (a^b)
- Count of values of x <= n for which (n XOR x) = (n - x)
- Find the values of X and Y in the Given Equations
- Count subsequences with same values of Bitwise AND, OR and XOR
- Number of subsets with same AND, OR and XOR values in an Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.