← Back

26. Conditional Logic Without If-Else

Medium

📝 Problem

Accept a number as input which will be 1 or 2, If it is 1, print 2. If it is 2, print 1.
(without using conditional statement)

Input Format:
Accept an integer value as input
Output Format:
If input is 1, print "2", if input is 2, print "1"
Constraints:
without using conditional statement
Sample Input 1:
2
Sample Output 1:
1
Sample Input 2:
1
Sample Output 2:
2
🎯 Think Outside the Box: Solving Conditionals with Pure Math!

The Challenge: No if-else Allowed!

This problem is a mind-bending puzzle. Usually, we'd use an if-else statement to swap 1↔2. But what if conditionals don't exist? Welcome to the world of mathematical logic where clever arithmetic replaces branching!

💡 Why This Question is Special
This problem teaches you to think algorithmically. In basic I/O, you don't have control structures yet! The solution requires recognizing patterns and using simple math to achieve complex logic. This skill is crucial for optimization and understanding how computers work at a fundamental level.

1️⃣ The "Obvious" Way (NOT Allowed Here!)

📌 ❌ Using if-else (The Way We CAN'T Use):
int a;
cin >> a;

// ❌ This would work BUT uses conditional!
if (a == 1) {
    cout << 2;
} else {
    cout << 1;
}

2️⃣ The Mathematical Solution: Finding the Pattern

Let's analyze the problem:

Input (a) Output Relationship?
1 2 🤔
2 1 🤔

What pattern can we find?

Notice that: 1 + 2 = 3 and 2 + 1 = 3

So if we have one number, the other is: 3 - number

📌 The Mathematical Formula:
// If input is 1: 3 - 1 = 2 ✓
// If input is 2: 3 - 2 = 1 ✓

int a;
cin >> a;
cout << 3 - a;  // One line solves it all!
✨ The Beauty: A single arithmetic operation (3 - a) replaces an entire conditional structure! This is the elegance of mathematical thinking.

3️⃣ Step-by-Step Logic Breakdown

📌 How It Works:
// Example 1: Input = 2
a = 2
Output = 3 - 2 = 1// Example 2: Input = 1
a = 1
Output = 3 - 1 = 2// Why does this work?
// Because we're finding the "complement" in the set {1, 2}
// The sum of both numbers is always 3
// So: other_number = total - current_number

4️⃣ Alternative Mathematical Approaches

There are multiple ways to solve this without conditionals:

📌 Method 1: Subtraction (Our Solution) ✅
cout << 3 - a;  // Simplest and most readable
📌 Method 2: XOR with 3
cout << (a ^ 3);  // Bitwise magic!
// 1 ^ 3 = 2 (binary: 01 ^ 11 = 10)
// 2 ^ 3 = 1 (binary: 10 ^ 11 = 01)
📌 Method 3: Modulo Arithmetic
cout << (a % 2) + 1;  // Toggle between 1 and 2
// 1 % 2 = 1, then +1 = 2
// 2 % 2 = 0, then +1 = 1
📌 Method 4: Negation and Offset
cout << -a + 3;  // Same as 3 - a
📌 Method 5: Array Lookup (Clever!)
int lookup[] = {0, 2, 1};  // index 0 unused, 1→2, 2→1
cout << lookup[a];

5️⃣ Comparison of Methods

Method Formula Readability Performance
Subtraction 3 - a ⭐⭐⭐⭐⭐ ⚡ Fastest
XOR a ^ 3 ⭐⭐⭐ ⚡ Fastest
Modulo (a % 2) + 1 ⭐⭐⭐⭐ ⚡ Fast
Array Lookup lookup[a] ⭐⭐⭐ ⚡ Fast

6️⃣ Generalizing the Pattern

What if we had different numbers?

📌 General Formula for Toggling:
// To toggle between two numbers X and Y:
// Formula: (X + Y) - current

// Toggle between 5 and 10:
Output = (5 + 10) - a;  // = 15 - a
// If a=5: 15-5=10 ✓
// If a=10: 15-10=5 ✓

// Toggle between 0 and 1 (common in binary):
Output = 1 - a;  // or use: a ^ 1

7️⃣ Real-World Applications

This technique appears in many contexts:

  • Binary Toggle: Switching between 0 and 1 (on/off states)
  • Graphics Programming: Alternating between two colors/states
  • Game Development: Player turn switching (Player 1 ↔ Player 2)
  • Embedded Systems: No branching = faster, predictable execution
  • State Machines: Simple two-state toggles
  • Optimization: Branch-free code can be faster on modern CPUs
🚀 Why Branch-Free Code Matters
Modern CPUs use branch prediction to optimize code. When predictions fail, it causes slowdowns. Mathematical solutions like 3 - a avoid branches entirely, leading to:
  • Predictable performance
  • No pipeline stalls
  • Easier compiler optimization
  • Better for parallel processing

💡 The Lesson: Creative Problem Solving

This problem teaches a crucial programming skill: recognizing patterns. When you can't use the "obvious" solution, step back and look for mathematical relationships. Often, the most elegant solution is the simplest one!

🎓 Key Takeaway: Before reaching for complex control structures, ask yourself: "Can I solve this with simple arithmetic?" Sometimes, the answer is a beautiful one-liner like 3 - a!
💻 Solution Code
#include <iostream>

using namespace std;

int main() {
    int a;
    
    cin >> a;
    
    cout << 3 - a;
    
    return 0;
}