Swap Two Numbers in Deluge | With and Without Using a Third Variable | Beginner Friendly Tutorial

Swap Two Numbers in Deluge | With and Without Using a Third Variable | Beginner Friendly Tutorial

Introduction

Here is a situation that comes up more than you would expect when writing scripts: you have two variables holding two values, and you need to exchange them. Variable a should end up with what b had, and b should end up with what a had. Simple idea, but if you just write a = b and then b = a, it does not work. By the time you execute the second line, a has already lost its original value.

This is actually one of the first real “aha” moments in learning to program — the computer executes things one line at a time, so order matters far more than it seems. In this post I want to walk through two different ways to swap variables in Deluge, explain the logic behind each one clearly, and make sure you come away understanding why they work, not just what to type.


What We Are Trying to Do

Let us say we start here:

Before Swap a = 5    b = 10
After Swap a = 10    b = 5

Straightforward enough on paper. But here is why the obvious approach fails in code — and this is worth really sitting with, because understanding this unlocks both methods.

If you try this:

a = 5;
b = 10;

a = b;  // a is now 10. But the original value of a (5) is gone forever.
b = a;  // b is now 10 too. We just copied b into itself.

// Result: a = 10, b = 10. NOT what we wanted.

The original value of a gets overwritten the moment you write a = b. It is gone. The second line then just assigns the already-overwritten value of a to b, so you end up with both variables holding 10. This is the core problem that both methods below solve, each in its own way.


Method 1: Using a Temporary Variable

The classic solution is to use a third variable as a holding area — like putting something on a shelf while you rearrange two boxes. We temporarily park the value of a somewhere safe, then carry out the swap.

Think of it like swapping two drinks between two glasses. You cannot pour one directly into the other without spilling. The fix is obvious — grab an empty third glass, pour one drink in there, then do the transfer cleanly.

The Three Steps

1
Save a into temp before anything else changes: temp = a
2
Overwrite a with the value of b — safe now because a’s original value is already saved: a = b
3
Assign the saved value from temp into b: b = temp

The Deluge Code

a = 5;
b = 10;

// Step 1: Save a's original value before it gets overwritten
temp = a;

// Step 2: Give a the value of b
a = b;

// Step 3: Give b the saved original value of a
b = temp;

info "a = " + a;  // Output: a = 10
info "b = " + b;  // Output: b = 5

Tracing Through It Step by Step

Step Operation a b temp
Start Initial values 5 10
1 temp = a 5 10 5
2 a = b 10 10 5
3 b = temp 10 5 5

The trace makes it clear why this works. The moment we write temp = a on Step 1, a’s original value (5) is safely stored. Everything after that is just moving values around without any risk of losing data.


Method 2: Swapping Without Any Extra Variable

This one is a bit more clever. What if you did not want to use a third variable at all? It turns out you can pull off the swap using only addition and subtraction — no temp needed. The trick is to encode information about both values inside one of the existing variables temporarily.

Here is the intuition: if you add a and b together and store the result in a, then a now contains enough information to recover both original values. You can always get the old b by subtracting the old a from the sum, and vice versa. Let us see that in action.

The Three Steps

1
Add both values and store the total in a: a = a + b. Now a holds the sum of both original numbers.
2
Subtract current b from current a (the sum) and store in b: b = a - b. This gives b the original value of a.
3
Subtract the new b (which is now the original a) from current a (the sum): a = a - b. This leaves a with the original value of b.

The Deluge Code

a = 5;
b = 10;

// Step 1: Store the sum of both in a
a = a + b;  // a is now 15, b is still 10

// Step 2: Recover the original a by subtracting original b from the sum
b = a - b;  // b = 15 - 10 = 5  (original a)

// Step 3: Recover the original b by subtracting the new b from the sum
a = a - b;  // a = 15 - 5 = 10  (original b)

info "a = " + a;  // Output: a = 10
info "b = " + b;  // Output: b = 5

Tracing Through It Step by Step

Step Operation Calculation a b
Start Initial values 5 10
1 a = a + b 5 + 10 = 15 15 10
2 b = a - b 15 - 10 = 5 15 5
3 a = a - b 15 - 5 = 10 10 5

The key insight is Step 2. At that point a holds the sum (15) and b still holds the original 10. Subtracting 10 from 15 gives us 5 — the original value of a. Step 3 then subtracts the recovered value (5) from the sum (15) to get 10 — the original value of b. Elegant once it clicks.


Which Method Should You Use?

Both methods produce identical results. The choice depends on the situation and how readable you need your code to be.

Temp Variable No Temp Variable
Readability Very clear — anyone can follow it Takes a moment to trace through
Extra memory Uses one extra variable No extra variable needed
Works with all types? Yes — numbers, strings, anything Numbers only (arithmetic required)
Risk of overflow? None Possible with very large numbers
Best for Everyday scripts, team codebases Interview questions, space-constrained situations

Honestly, in day-to-day Deluge scripting inside Zoho, Method 1 is what you will reach for. It is immediately obvious to anyone reading the code what is happening. Method 2 is the kind of thing you want to understand deeply — both because it is a classic interview question and because working through the arithmetic teaches you something real about how variables and memory work.

One important limitation of Method 2 worth flagging: it only works with numbers. If a and b were strings, the addition step would concatenate them rather than sum them, and the whole thing falls apart. For anything other than numeric values, Method 1 is your only option.


Mistakes That Are Easy to Make Here

Swapping without saving first

Writing a = b; b = a; without a temp variable is the most common beginner mistake. As we saw at the top, the moment you assign b to a, the original value of a is lost. Always save first, then swap.

Getting the order of operations wrong in Method 2

The three lines in Method 2 must run in exactly the right order. If you swap lines 2 and 3, you will get wrong results. Trace through with actual numbers any time you are unsure — it takes 30 seconds and saves a lot of debugging.

Trying Method 2 with strings or non-numeric values

Method 2 is strictly for numbers. Using it on strings will produce concatenated nonsense rather than a swap. Always use Method 1 when the variables might contain anything other than plain numbers.


Wrapping Up

Swapping two variables is one of those foundational programming problems that every developer works through early on. It seems trivial until you actually try to do it naively and watch it break. Understanding why a direct assignment does not work — and why both of these methods do — is the kind of thinking that makes you a better scripter across the board.

In Deluge specifically, you will find the temp variable approach shows up regularly whenever you need to reorder list items, rotate values in a workflow, or just cleanly exchange two fields in a record update. Having this muscle memory of saving before overwriting will save you from a class of bugs that can be surprisingly hard to spot after the fact.

Try both methods in Zoho’s function editor with a few different pairs of numbers to make sure the logic is solid in your head. Then try to explain Method 2 to yourself without looking at the code — if you can do that, you have genuinely understood it.

Post a Comment