Introduction to Pseudocode
1. What is Pseudocode?
• Definition: Pseudocode is a simplified, half-code, half-English textual representation of a
programming algorithm. It is not executed on computers but helps programmers plan and
discuss algorithms.
• Purpose: The main goal is to focus on the algorithm's logic without worrying about
syntax rules of a specific programming language.
• Audience: Useful for programmers, developers, and anyone learning programming
concepts.
2. Characteristics of Pseudocode
• Simplicity: Easy to read and write by humans.
• Language Independence: Not bound by the syntax of any programming language.
• Flexibility: Allows for the expression of algorithms in a way that best communicates the
intended logic.
3. Basic Syntax and Conventions
While pseudocode does not have a strict syntax, some common conventions are often followed:
• Variables: Write in plain English, often avoiding special characters. E.g., totalPrice,
count.
• Operations: Use common mathematical symbols (+, -, *, /) and simple English words
for other operations (AND, OR, NOT).
• Control Structures: Keywords for control structures (e.g., IF, THEN, ELSE, FOR,
WHILE) are capitalized to distinguish them from variables.
4. Common Pseudocode Constructs
4.1. Variables and Assignment
• Declaration: Implicitly done when the variable is first used.
• Assignment: Use the = symbol, e.g., total = 0.
4.2. Input and Output
• Input: READ or INPUT, e.g., READ userName.
• Output: PRINT or WRITE, e.g., PRINT total.
4.3. Conditional Statements
IF condition THEN
statement(s)
ELSE
statement(s)
END IF
4.4. Loops
For Loop:
FOR index FROM startValue TO endValue DO
statement(s)
END FOR
While Loop:
WHILE condition DO
statement(s)
END WHILE
5. Example: Algorithm to Find the Largest Number in a List
1. START
2. READ list of numbers
3. SET largestNumber TO list[0]
4. FOR EACH number IN list DO
IF number > largestNumber THEN
SET largestNumber TO number
END IF
5. END FOR
6. PRINT largestNumber
7. END
6. Example: Write pseudocode to calculate the sum of all numbers from 1 to N
Procedure calculateSum(N):
// Initialize a variable to store the sum
sum = 0
// Iterate from 1 to N
For i from 1 to N do
// Add the current value of i to the sum
sum = sum + i
EndFor
// Output the sum
Display "The sum of numbers from 1 to " + N + " is " + sum
EndProcedure
7. pseudocode example for a simple login system:
Procedure loginSystem:
// Prompt the user to enter their username
Display "Enter your username:"
Input username
// Prompt the user to enter their password
Display "Enter your password:"
Input password
// Check if the entered username and password are valid
If username is equal to "admin" and password is equal to "password123"
then
Display "Login successful. Welcome, admin!"
Else
Display "Invalid username or password. Please try again."
EndIf
EndProcedure
8. pseudocode example that reads a list of numbers and calculates their average:
Procedure calculateAverage:
// Initialize a list to store the numbers
numbersList = []
// Initialize variables to store the sum of numbers and count
sum = 0
count = 0
// Prompt the user for the number of numbers
Display "How many numbers do you want to enter?"
Input count
// Loop to read numbers from the user
For i from 1 to count do
Display "Enter number " + i + ":"
Input number
// Add the entered number to the list
Append number to numbersList
EndFor
// Calculate the sum of the numbers in the list
For each number in numbersList do
sum = sum + number
EndFor
// Calculate the average
If count > 0 then
average = sum / count
Display "The average of the entered numbers is: " + average
Else
Display "No numbers were entered."
EndIf
EndProcedure