0% found this document useful (0 votes)
12 views6 pages

How To Write Pseudocode

Pseudocode is a simplified way to outline the steps of a program using plain English, focusing on logic rather than syntax. It serves as a blueprint for programming, making it easier to write actual code later. Key points include using clear language, control words for flow, proper indentation, and keeping instructions simple and sequential.

Uploaded by

adityansut613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

How To Write Pseudocode

Pseudocode is a simplified way to outline the steps of a program using plain English, focusing on logic rather than syntax. It serves as a blueprint for programming, making it easier to write actual code later. Key points include using clear language, control words for flow, proper indentation, and keeping instructions simple and sequential.

Uploaded by

adityansut613
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

What is 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.

Why do we use Pseudocode?

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.

Important Points to Follow When Writing Pseudocode

1. Use clear and simple words

 Write each step in very easy and simple English.

 Anyone reading it should easily understand what is happening.

 Example:
Instead of writing "Initialize an integer variable to zero," just say "Set count = 0."

Simple = Easy to understand.

2. Use control words (important keywords)

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.

 PROCEDURE and FUNCTION:


Used to show that a part of the program is grouped into a small named block that can be
called whenever needed.

In short:
Control words help you organize and control the flow of steps.

3. Indent properly when steps are inside other 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

PRINT "Positive number"

ENDIF

Here, "PRINT" is indented under "IF", so it is clear it will happen only if the condition is true.

Indentation makes the structure easy to read and easy to understand.

4. Show the flow of logic properly

 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:

 First boil water.

 Then add tea leaves.

 Then add milk.

 Then sugar.

 Then filter and serve.

Each step is in order. Similarly, your program steps should also be in correct and clear order.

5. Keep each instruction in one simple step

 Do not mix too many actions in one line.


 Each line should do only one small task.

Example (good way):

Set total = 0

FOR each item in list

Add item price to total

ENDFOR

Example (bad way):

Set total = 0 and loop through all items and add price to total

(Bad because it mixes too much in one line.)

Example 1: Merging Two Sorted Linked Lists

Suppose you have two linked lists:

 List1: 1 → 3 → 5

 List2: 2 → 4 → 6

We want to merge them into one sorted linked list:

 Result: 1 → 2 → 3 → 4 → 5 → 6

Pseudocode:

PROCEDURE MergeLists(List1, List2)

CREATE a new dummy node called Dummy

SET Tail = Dummy

WHILE List1 is not NULL AND List2 is not NULL DO

IF List1.Data < List2.Data THEN

Tail.Next = List1

List1 = List1.Next

ELSE

Tail.Next = List2
List2 = List2.Next

ENDIF

Tail = Tail.Next

ENDWHILE

IF List1 is not NULL THEN

Tail.Next = List1

ELSE

Tail.Next = List2

ENDIF

RETURN Dummy.Next

ENDPROCEDURE

Detailed Explanation :

1. We create a dummy node called Dummy.


It is a fake starting node to make our logic easier.
(Why? Because then we don't need to check separately if the merged list is empty.)

2. We create a pointer Tail that initially points to Dummy.


Tail will always point to the last node of our new merged list.

3. Now, while both List1 and List2 are not empty, we do the following:

o Compare the current values of List1 and List2.

o If the value in List1 is smaller:

 Attach List1 to Tail.Next.

 Move List1 one step ahead (List1 = List1.Next).

o Otherwise:

 Attach List2 to Tail.Next.

 Move List2 one step ahead (List2 = List2.Next).

o After attaching a node, move Tail ahead (Tail = Tail.Next).

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.

o Otherwise, attach the remaining List2 to Tail.Next.


5. Finally, we return Dummy.Next, which points to the real head of the merged list.
(We do not return Dummy because it was just a fake starting point.)

Summary:

 Compare elements from both lists one by one.

 Always pick the smaller one and move forward.

 If one list ends first, directly join the remaining part of the other list.

Example 2: Tower of Hanoi Problem

Problem description:

 There are N disks placed on a rod (called Source rod).

 The disks are placed in decreasing size from bottom to top (largest at the bottom).

 We have two more rods: Auxiliary rod and Destination rod.

 The task is to move all disks from Source rod to Destination rod, using Auxiliary rod for help.

Rules:

 Only one disk can be moved at a time.

 A larger disk cannot be placed on a smaller disk.

Pseudocode:

PROCEDURE TowerOfHanoi(N, Source, Auxiliary, Destination)

IF N == 1 THEN

PRINT "Move disk 1 from " Source " to " Destination

ELSE

TowerOfHanoi(N-1, Source, Destination, Auxiliary)

PRINT "Move disk " N " from " Source " to " Destination

TowerOfHanoi(N-1, Auxiliary, Source, 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.

o This is the simplest case. No more recursion needed for this.

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.

 Here, Destination rod is used as a helper.

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.

 Now, Source rod becomes the helper.

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.

You might also like