Week 2: Pseudocode Basics
2368 words
12 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Week 2: Pseudocode Basics > **BSCS1001 — IIT Madras BS Degree** **Prerequisite:** Week 1 (Flowcharts, Iteration) **Cross-links:** BSCS1002-Python (Week 2 — Control Flow), BSCS2002-PDSA (Week 1 — Algorithms) ## 1. Motivation: From Pictures to Text In Week 1, we used **flowcharts** to visualize algorithms.

Week 2: Pseudocode Basics
BSCS1001 — IIT Madras BS Degree Prerequisite: Week 1 (Flowcharts, Iteration) Cross-links: BSCS1002-Python (Week 2 — Control Flow), BSCS2002-PDSA (Week 1 — Algorithms)
1. Motivation: From Pictures to Text
In Week 1, we used flowcharts to visualize algorithms. Flowcharts are great for understanding, but they have problems:
| Problem with Flowcharts | Solution |
|---|---|
| Large algorithms need huge diagrams | Text is compact |
| Hard to share (images) | Text can be copied, emailed, version-controlled |
| Difficult to edit | Text can be searched and replaced |
| No standard detail level | Pseudocode has conventions |
Pseudocode is the middle ground: it looks like a programming language but is meant for humans to read. It's the language of this course.
💡 Key Insight: Think of pseudocode as "programming in English" — precise enough for a computer, but readable by a human.
The Evolution
(Diagram)
In this course, we stop at pseudocode. In BSCS1002 (Python), you'll learn to convert pseudocode to actual Python code.
2. What is Pseudocode?
Pseudocode is a compact, textual notation for describing algorithms. It uses:
- Standard phrases like
while,if,else - Mathematical notation for operations
- Indentation (or braces
{}) to show blocks - Dot notation (
.) for record field access
Example: Counting Cards
Flowchart version (Week 1):
(Diagram)
Pseudocode version:
sqlCount = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 Count = Count + 1 }
Notice: The pseudocode is shorter, more precise, and easier to modify than the flowchart. But both describe the same algorithm.
Pseudocode Conventions in This Course
| Convention | Meaning | Example |
|---|---|---|
= | Assignment (not equality!) | Sum = 0 |
== | Equality check | if (X.Gender == "M") |
{ } | Block of statements | while (condition) { ... } |
. | Field access on a record | X.Maths, X.Name |
// or nothing | Comments | // This is a comment |
3. Core Pseudocode Constructs
Pseudocode has just a few building blocks. Once you learn these, you can write any algorithm.
(Diagram)
| Construct | Purpose | Example |
|---|---|---|
| Assignment | Store or update a value | Sum = Sum + X.Maths |
| Sequence | Steps execute in order | Pick X then Move X then Update |
| Conditional | Make a decision | if (X.Gender == "M") |
| Iteration | Repeat a block | while (Pile 1 has more cards) |
4. Assignment Statement
The assignment statement is the most basic operation. It stores a value in a variable.
Syntax
pseudoVariableName = Expression
How It Works
- Evaluate the expression on the right side using current variable values
- Store the result in the variable on the left side
Examples
| Statement | What Happens |
|---|---|
Count = 0 | Store the value 0 in Count |
Sum = Sum + 85 | Take current Sum, add 85, store back in Sum |
Max = X.Maths | Copy the Maths field of card X into Max |
Found = True | Store the Boolean value True in Found |
Tracing Assignment
Suppose
Sum = 50 initially.| Statement | Right Side Evaluation | New Value of Sum |
|---|---|---|
Sum = Sum + 10 | 50 + 10 = 60 | 60 |
Sum = Sum + 5 | 60 + 5 = 65 | 65 |
Sum = Sum / 2 | 65 / 2 = 32.5 | 32.5 |
⚠️ Important: The=sign is not mathematical equality.Sum = Sum + 1does NOT mean "Sum equals Sum plus 1" (which is impossible in math). It means "increase Sum by 1."
5. Conditional Execution
Conditional execution lets the algorithm make decisions.
If Statement
pseudoif (condition) { Statement 1 Statement 2 ... }
The block inside
{ } executes only if the condition is True.If-Else Statement
pseudoif (condition) { Statements for True case } else { Statements for False case }
Flowchart of If-Else
(Diagram)
Worked Example: Separate Boy/Girl Sums
sqlBoySum = 0 GirlSum = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X.Gender == "M") { BoySum = BoySum + X.Maths } else { GirlSum = GirlSum + X.Maths } }
Tracing with dataset:
| Card | Name | Gender | Maths | Condition | Action | BoySum | GirlSum |
|---|---|---|---|---|---|---|---|
| — | — | — | — | Initial | — | 0 | 0 |
| 1 | Alice | F | 85 | F == M? ❌ | Else: GirlSum += 85 | 0 | 85 |
| 2 | Bob | M | 72 | M == M? ✅ | If: BoySum += 72 | 72 | 85 |
| 3 | Charlie | M | 91 | M == M? ✅ | If: BoySum += 91 | 163 | 85 |
| 4 | Diana | F | 68 | F == M? ❌ | Else: GirlSum += 68 | 163 | 153 |
Result: BoySum = 163, GirlSum = 153
Nested Conditionals
Conditions can be nested inside other conditions:
pseudoif (X.Gender == "M") { if (X.Maths > 80) { Count = Count + 1 } }
This counts only boys who scored above 80 in Maths.
6. Iteration with while
The
while loop is the primary iteration construct in our pseudocode.Syntax
pseudowhile (condition) { // Block of statements to repeat }
How It Works
- Check the condition
- If True → execute the block, then go back to step 1
- If False → exit the loop and continue after
}
Flowchart of While Loop
(Diagram)
The Standard Dataset Iteration Pattern
Almost every algorithm in Weeks 1-4 follows this pattern:
sqlInitialize variables while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 // Update variables based on X }
Why "Pile 1 has more cards"?
This is the course's standard way of saying "there are still unprocessed items." It's a condition that:
- Starts as
True(if the dataset is non-empty) - Becomes
Falseafter the last card is moved - Guarantees every card is processed exactly once
7. Equality vs Assignment
This is the most common source of confusion for beginners. Let's be very clear:
| Symbol | Name | Meaning | Example |
|---|---|---|---|
= | Assignment | Store right value in left variable | Sum = 0 |
== | Equality | Compare two values, return Boolean | if (X.Gender == "M") |
How to Read Them Aloud
| Written | Read As |
|---|---|
Count = 0 | "Count becomes 0" or "Set Count to 0" |
Count == 0 | "Count equals 0?" or "Is Count equal to 0?" |
Why Two Different Symbols?
Consider this common mistake:
pseudo// WRONG - this is assignment, not comparison! if (X.Gender = "M") {
This would set X.Gender to "M" instead of checking if it is "M" — completely changing the data! The course wisely uses
== for comparison to avoid this catastrophe.Memory Aid
Think of = as | Think of == as |
|---|---|
| An arrow ← | A question mark ? |
| "Goes to" | "Is equal to?" |
| A command | A test |
| Changes the world | Asks a question |
8. Complete Pseudocode Examples
Example 1: Sum of All Maths Marks
sqlSum = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 Sum = Sum + X.Maths }
Dataset: [Alice: 85, Bob: 72, Charlie: 91, Diana: 68]
| Iteration | Card | X.Maths | Sum Before | Sum After |
|---|---|---|---|---|
| 1 | Alice | 85 | 0 | 85 |
| 2 | Bob | 72 | 85 | 157 |
| 3 | Charlie | 91 | 157 | 248 |
| 4 | Diana | 68 | 248 | 316 |
Final Sum: 316
Example 2: Finding Maximum with Card ID
sqlMaxM = 0 MaxCard = -1 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X.Maths > MaxM) { MaxM = X.Maths MaxCard = X.Id } }
Dataset: [ID1:45, ID2:78, ID3:62, ID4:91, ID5:53]
| Iter | X.Id | X.Maths | MaxM Before | Condition | MaxM After | MaxCard |
|---|---|---|---|---|---|---|
| 1 | 1 | 45 | 0 | 45>0 ✅ | 45 | 1 |
| 2 | 2 | 78 | 45 | 78>45 ✅ | 78 | 2 |
| 3 | 3 | 62 | 78 | 62>78 ❌ | 78 | 2 |
| 4 | 4 | 91 | 78 | 91>78 ✅ | 91 | 4 |
| 5 | 5 | 53 | 91 | 53>91 ❌ | 91 | 4 |
Result: Max Maths = 91, achieved by Student ID = 4.
Example 3: Below Average Students
This uses TWO iterations in sequence (non-nested):
sql// First pass: calculate average Sum = 0 Count = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 Sum = Sum + X.Maths Count = Count + 1 } Average = Sum / Count // Second pass: find below-average students BelowCount = 0 while (Pile 2 has more cards) { // Note: cards are now in Pile 2! Pick a card X from Pile 2 Move X to Pile 1 // Moving back to Pile 1 if (X.Maths < Average) { BelowCount = BelowCount + 1 } }
| Step | What's Happening | Variables |
|---|---|---|
| Pass 1 | Sum all marks, count students | Sum = 316, Count = 4 |
| After Pass 1 | Calculate average | Average = 316/4 = 79 |
| Pass 2 | Count marks < 79 | BelowCount = 2 (Bob:72, Diana:68) |
9. Flowchart to Pseudocode Translation
Translation Guide
| Flowchart Element | Pseudocode Equivalent |
|---|---|
| Oval (Start/End) | Implicit (start at top, end at bottom) |
| Rectangle (Process) | Assignment statement |
| Diamond (Decision) | if (condition) { ... } |
| Loop back arrow | while (condition) { ... } |
| Arrow paths | Sequence (top to bottom) |
Worked Translation
Flowchart:
(Diagram)
Pseudocode:
sqlSum = 0 Count = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 Sum = Sum + X.Maths Count = Count + 1 } Average = Sum / Count
💡 Tip: The loop back arrow in the flowchart maps directly to thewhilekeyword. Everything inside the loop body is what's inside the rectangle and diamond shapes in the loop path.
10. Practice Questions
Basic Questions
Q1. What is the value of
x after this pseudocode executes?pseudox = 10 x = x + 5 x = x * 2
Show Answer
| Step | Statement | x |
|---|---|---|
| 1 | x = 10 | 10 |
| 2 | x = 10 + 5 = 15 | 15 |
| 3 | x = 15 × 2 = 30 | 30 |
Q2. What is the difference between
= and == in pseudocode?Show Answer=is assignment (store a value).==is equality comparison (check if two values are the same, returns True/False). Q3. Trace this pseudocode for input values [4, 8, 2, 6]:
sqlSum = 0 Count = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X > 5) { Sum = Sum + X } Count = Count + 1 }
Show Answer
| Iter | X | X > 5? | Sum | Count |
|---|---|---|---|---|
| Start | — | — | 0 | 0 |
| 1 | 4 | ❌ No | 0 | 1 |
| 2 | 8 | ✅ Yes | 8 | 2 |
| 3 | 2 | ❌ No | 8 | 3 |
| 4 | 6 | ✅ Yes | 14 | 4 |
Final: Sum = 14, Count = 4 Q4. Write pseudocode to find the minimum value in a dataset. Show AnswersqlMin = INFINITY // Very large number while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X < Min) { Min = X } }Alternatively, initializeMinto the first card's value.
Intermediate Questions
Q5. Convert this flowchart to pseudocode:
(Diagram)
Show AnswersqlTotal = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X.Gender == "F" AND X.Maths > 70) { Total = Total + X.Maths } }
Q6. What happens if you forget to move the card from Pile 1 to Pile 2?
Show AnswerIf you don't move the card, the same card will be picked again in the next iteration! The conditionPile 1 has more cardswill remain True forever, creating an infinite loop — the algorithm never ends.This is whyMove X to Pile 2is a critical step in every iteration. Q7. Write pseudocode that counts the number of students who scored between 60 and 80 (inclusive) in Maths. Show AnswersqlCount = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X.Maths >= 60 AND X.Maths <= 80) { Count = Count + 1 } }
Q8. What is wrong with this pseudocode?
sqlSum = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Sum = Sum + X.Maths }
Show AnswerThe card X is never moved to Pile 2. The same card will be picked repeatedly, creating an infinite loop. The fix is to addMove X to Pile 2after picking the card.
Advanced Questions
Q9. Write pseudocode to find both the maximum and minimum values in a single pass through the data.
Show AnswersqlMax = -INFINITY Min = INFINITY while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X > Max) { Max = X } if (X < Min) { Min = X } }This requires only one pass (one iteration) to find both. Q10. Trace the execution: What does this pseudocode output for dataset [2, 5, 1, 8, 3]?
sqlResult = 0 while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 if (X > Result) { Result = X } else { Result = Result - X } }
Show Answer
| Iter | X | Condition | Action | Result |
|---|---|---|---|---|
| Start | — | — | — | 0 |
| 1 | 2 | 2 > 0 ✅ | Result = 2 | 2 |
| 2 | 5 | 5 > 2 ✅ | Result = 5 | 5 |
| 3 | 1 | 1 > 5 ❌ | Result = 5 - 1 = 4 | 4 |
| 4 | 8 | 8 > 4 ✅ | Result = 8 | 8 |
| 5 | 3 | 3 > 8 ❌ | Result = 8 - 3 = 5 | 5 |
Final Result = 5 Q11. What is the "standard dataset iteration pattern"? Why is every piece necessary? Show AnswerThe standard pattern is:sqlInitialize variables while (Pile 1 has more cards) { Pick a card X from Pile 1 Move X to Pile 2 // Process X }Each piece is necessary:
Initialize: Without this, variables start with garbage values Check condition: Ensures we stop when done Pick a card: Gets the next item Move to Pile 2: Ensures progress (prevents infinite loop) Process: Does the actual work Q12. Compare and contrast: flowchart vs pseudocode. When would you use each? Show Answer
| Aspect | Flowchart | Pseudocode |
|---|---|---|
| Format | Visual (diagram) | Textual |
| Ease of understanding | Excellent for beginners | Good |
| Compactness | Bulky for large algorithms | Compact |
| Share/edit | Difficult (images) | Easy (text) |
| Version control | Not practical | Yes (git) |
| Best for | Teaching, small algorithms | Complex algorithms, documentation |
Use flowcharts when explaining to beginners or debugging logic visually. Use pseudocode when writing real algorithms, collaborating, or preparing to code.
📚 Cross-References
| Course | Topic | Connection |
|---|---|---|
| BSCS1002 (Python) | Week 2 — If/Else | Python if, else |
| BSCS1002 (Python) | Week 2 — While loops | Python while |
| BSCS2002 (PDSA) | Week 1 — Algorithm representation | Algorithm notation |
Next Topic: 04 — Iteration & FilteringQuiz Tip: Quiz 1 heavily tests your ability to trace pseudocode. Practice with the tracing tables! Join Discord PreviousData Types & RepresentationNextIteration & Filtering