How To Write Pseudocode
How To Write Pseudocode
Pseudocode is a way of writing down the steps of a program in simple English, but in an organized
way so that it looks like a real program.
In pseudocode, you do not have to worry about writing correct programming language rules (syntax).
You just need to focus on what the program should do, step-by-step, clearly and neatly.
Before actually writing a real program (in C, Java, Python, etc.), it is better to plan how the program
will work.
Pseudocode helps you to think clearly about the logic.
It acts like a blueprint (plan) for your actual program.
If your pseudocode is correct and clear, writing the real program becomes very easy later.
Example:
Instead of writing "Initialize an integer variable to zero," just say "Set count = 0."
Some special words are used to show the control flow (decision-making, repetition, calling functions,
etc.):
IF-THEN-ELSE:
Used when you have to make a decision.
(If a condition is true, do something. Otherwise, do something else.)
WHILE:
Used when you want to repeat something as long as a condition is true.
FOR:
Used when you want to repeat something a specific number of times.
REPEAT-UNTIL:
Repeat the steps until a condition becomes true.
In short:
Control words help you organize and control the flow of steps.
Whenever a step is inside another step (for example, inside an IF or inside a loop), you
should move it a little to the right (indent it).
This shows clearly that these steps belong inside a bigger step.
Example:
IF x > 0 THEN
ENDIF
Here, "PRINT" is indented under "IF", so it is clear it will happen only if the condition is true.
Your pseudocode should clearly show what happens first, what happens next, and so on.
Anyone reading your pseudocode should be able to understand the entire working of the
program step-by-step without getting confused.
Think of it like giving someone very clear instructions on how to make tea:
Then sugar.
Each step is in order. Similarly, your program steps should also be in correct and clear order.
Set total = 0
ENDFOR
Set total = 0 and loop through all items and add price to total
List1: 1 → 3 → 5
List2: 2 → 4 → 6
Result: 1 → 2 → 3 → 4 → 5 → 6
Pseudocode:
Tail.Next = List1
List1 = List1.Next
ELSE
Tail.Next = List2
List2 = List2.Next
ENDIF
Tail = Tail.Next
ENDWHILE
Tail.Next = List1
ELSE
Tail.Next = List2
ENDIF
RETURN Dummy.Next
ENDPROCEDURE
Detailed Explanation :
3. Now, while both List1 and List2 are not empty, we do the following:
o Otherwise:
4. After the loop finishes, it means at least one list is now empty.
o If List1 still has some nodes left, attach the entire remaining List1 to Tail.Next.
Summary:
If one list ends first, directly join the remaining part of the other list.
Problem description:
The disks are placed in decreasing size from bottom to top (largest at the bottom).
The task is to move all disks from Source rod to Destination rod, using Auxiliary rod for help.
Rules:
Pseudocode:
IF N == 1 THEN
ELSE
PRINT "Move disk " N " from " Source " to " Destination
ENDIF
ENDPROCEDURE
Detailed Explanation:
1. Base Case:
o If there is only one disk, then simply move it from the Source rod to the Destination
rod.
2. Recursive Case:
o If there are more than one disk (N > 1), follow three steps:
1. First, move the top N-1 disks from Source rod to Auxiliary rod.
2. Then, move the largest disk (Nth disk) from Source rod directly to
Destination rod.
3. Finally, move the N-1 disks from Auxiliary rod to Destination rod.
3. The key point here is that the same logic is applied repeatedly to smaller problems.
This is called recursion.
We reduce the problem size by 1 each time until it becomes 1, which is the base case.