Q.Write a program to swap two numbers without using a third variable.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Concept understanding — Variable Assignment Order
Variable Assignment Order — First Principles
Imagine you are following a recipe. The recipe says: "Take the number of eggs, call it . Then take , add 2, and call the result ." You would never dream of using before you have defined — that would be nonsense. Variable assignment order is simply the rule that a variable must be given a value before it can be used in any expression that defines another variable.
The Intuition
Think of a variable as a labelled box. You cannot take something out of a box that is empty. When you write:
you first put the number 5 into box , then you look into box , take out the 5, add 3, and put the result (8) into box . The order is: define first, use second.
Now consider what happens if you reverse the order:
At the moment you try to compute , box is empty. The expression has no meaning — it is like asking "what is nothing plus 3?" In most programming languages and in mathematical reasoning, this is an error. The variable has not yet been assigned. …
Unlock the full solution
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
7-day money-back guarantee · under 100 questions viewed (whichever comes first)