This is where it all comes together: you take an algorithm written in pseudocode and translate it into a real program. We will do every example in Python and C side by side, so you can see how each pseudocode statement becomes code.
Build on what you learned in Forms 1–3
These Lower School ICT posts cover the basics of this topic:
- Pseudocode, Looping, Revision – Live class (3NA 21-22) – writing pseudocode
- Scratch – Selection, Sensing, Repetition, Outputting Results. – selection and loops in Scratch
In this post:
- Declaring variables and constants
- A pseudocode-to-code translation table
- Worked example 1: sequence (average of 3 scores)
- Worked example 2: selection inside a WHILE loop (counting passes)
Declaring variables and constants
To declare a variable means to give it a name and (in many languages) a data type, so the computer sets aside memory for it.
- In C you must declare each variable with its type before using it:
int(integer),float(real),char(character),char name[20](string),bool(Boolean, with#include <stdbool.h>). Constants can be made with#define PASS_MARK 50orconst int PASS_MARK = 50;. - In Python a variable is created when you first assign a value to it, and Python works out the type:
age = 15(int),price = 12.5(float),name = "Kezia"(str),paid = True(bool). Constants are written in CAPITALS by convention:PASS_MARK = 50.
Python
# Declaring (creating) variables and a constant in Python
SCHOOL_NAME = "Island Secondary" # constant (by convention, CAPITALS)
age = 15 # integer
height = 1.62 # real (float)
grade = 'A' # character (a string of length 1)
name = "Kezia" # string
is_prefect = True # Boolean
print(name, "is", age, "years old and got grade", grade)
print("Height:", height, "m Prefect:", is_prefect, " School:", SCHOOL_NAME)
C
/* Declaring variables and a constant in C */
#include <stdio.h>
#include <stdbool.h>
#define SCHOOL_NAME "Island Secondary" /* constant */
int main(void)
{
int age = 15; /* integer */
float height = 1.62; /* real */
char grade = 'A'; /* character */
char name[20] = "Kezia"; /* string (array of characters) */
bool is_prefect = true; /* Boolean (1 = true, 0 = false) */
printf("%s is %d years old and got grade %c\n", name, age, grade);
printf("Height: %.2f m Prefect: %d School: %s\n", height, is_prefect, SCHOOL_NAME);
return 0;
}
Translation table
| Pseudocode | Python | C |
|---|---|---|
| READ age | age = int(input()) | scanf(“%d”, &age); |
| READ price | price = float(input()) | scanf(“%f”, &price); |
| PRINT “Total: “, total | print(“Total:”, total) | printf(“Total: %.2f\n”, total); |
| total ← 0 | total = 0 | total = 0; |
| IF x > 5 THEN … ELSE … ENDIF | if x > 5: … else: … | if (x > 5) { … } else { … } |
| FOR i ← 1 TO 10 DO … ENDFOR | for i in range(1, 11): | for (i = 1; i <= 10; i++) { … } |
| WHILE x <> -1 DO … ENDWHILE | while x != -1: | while (x != -1) { … } |
| a AND b, a OR b, NOT a | a and b, a or b, not a | a && b, a || b, !a |
| x MOD y, x DIV y | x % y, x // y | x % y, x / y (both integers) |
Remember: Python uses indentation (spaces) to show which lines belong inside an IF or a loop; C uses curly braces { } and ends each statement with a semicolon.
Worked example 1: average of three scores (sequence)
Pseudocode
START
READ score1, score2, score3
average ← (score1 + score2 + score3) / 3
PRINT "The average is ", average
STOP
Python
# Find the average of three test scores
score1 = float(input("Enter score 1: "))
score2 = float(input("Enter score 2: "))
score3 = float(input("Enter score 3: "))
average = (score1 + score2 + score3) / 3
print("The average is", format(average, ".2f"))
C
/* Find the average of three test scores */
#include <stdio.h>
int main(void)
{
float score1, score2, score3, average;
printf("Enter score 1: ");
scanf("%f", &score1);
printf("Enter score 2: ");
scanf("%f", &score2);
printf("Enter score 3: ");
scanf("%f", &score3);
average = (score1 + score2 + score3) / 3;
printf("The average is %.2f\n", average);
return 0;
}
Sample run: scores 60, 70 and 85 → The average is 71.67.
Worked example 2: counting passes (WHILE loop with selection)
Pseudocode
PASS_MARK ← 50
count ← 0
passed ← 0
READ mark
WHILE mark <> -1 DO
count ← count + 1
IF mark >= PASS_MARK THEN
PRINT "Pass"
passed ← passed + 1
ELSE
PRINT "Fail"
ENDIF
READ mark
ENDWHILE
PRINT passed, " out of ", count, " students passed"
Python
# Count how many students passed (mark of 50 or more).
# Enter -1 to stop.
PASS_MARK = 50
count = 0
passed = 0
mark = int(input("Enter mark (-1 to stop): "))
while mark != -1:
count = count + 1
if mark >= PASS_MARK:
print("Pass")
passed = passed + 1
else:
print("Fail")
mark = int(input("Enter mark (-1 to stop): "))
print(passed, "out of", count, "students passed")
C
/* Count how many students passed (mark of 50 or more).
Enter -1 to stop. */
#include <stdio.h>
#define PASS_MARK 50
int main(void)
{
int mark, count = 0, passed = 0;
printf("Enter mark (-1 to stop): ");
scanf("%d", &mark);
while (mark != -1)
{
count = count + 1;
if (mark >= PASS_MARK)
{
printf("Pass\n");
passed = passed + 1;
}
else
{
printf("Fail\n");
}
printf("Enter mark (-1 to stop): ");
scanf("%d", &mark);
}
printf("%d out of %d students passed\n", passed, count);
return 0;
}
Sample run with 72, 45, 50 then −1: Pass, Fail, Pass, then 2 out of 3 students passed. Notice that the mark of 50 (a boundary value) correctly gives “Pass”.
Try running these yourself. You can install Python (IDLE or Thonny) and a C compiler such as gcc or Code::Blocks, or use an online editor.
Try it: Pseudocode to code
Choose the correct Python or C statement for each pseudocode line.
| READ age (Python) | |
| PRINT total (C, total is an integer) | |
| IF mark >= 50 THEN (Python) | |
| WHILE n <> 0 DO (C) | |
| FOR i ← 1 TO 5 DO (Python) | |
| Declare a real variable called price (C) |
Related
Related: Demo: Constants, Variables and Datatypes.; Python Example: Indefinite prompting to determine the corresponding grade for a given mark
Paper 1 practice (multiple choice)
Choose the best answer for each question. Try them all before you check.
1. Which C statement declares an integer variable called count?
(A) count int;
(B) int count;
(C) integer count
(D) declare count
2. In Python, which statement reads a whole number from the user?
(A) num = input(int)
(B) num = int(input())
(C) scanf(num)
(D) READ num
3. The pseudocode operator <> is written in Python and C as
(A) =
(B) ==
(C) !=
(D) <>
4. Which C data type is used for a real number such as 12.75?
(A) int
(B) char
(C) float
(D) bool
5. In Python, the lines inside an if statement are shown by
(A) curly braces
(B) semicolons
(C) indentation
(D) ENDIF
Show answers
1. B
2. B
3. C
4. C
5. C
Paper 2 practice (structured question)
The following pseudocode finds the total cost of tickets for a school concert:
READ num_tickets
cost ← num_tickets * 40
IF num_tickets >= 10 THEN
cost ← cost - 50
ENDIF
PRINT cost
(a) Write a declaration, in a high-level language of your choice, for:
(i) the variable num_tickets (1 mark)
(ii) the variable cost (1 mark)
(b) Explain why the ticket price of $40 could be declared as a constant. (1 mark)
(c) Translate the pseudocode into a high-level language of your choice. (5 marks)
(d) State the output when num_tickets is 12. (2 marks)
Total: 10 marks
Show answers
(a)
(i) Int num_tickets; (in C) or num_tickets = 0 (in Python) (1 mark)
(ii) Int cost; or float cost; (in C) or cost = 0 (in Python) (1 mark)
(b) The price does not change while the program runs, so storing it as a constant makes the program easier to read and update (1 mark).
(c)
• Reading num_tickets correctly, e.g. num_tickets = int(input()) or scanf("%d", &num_tickets); (1 mark)
• cost = num_tickets * 40 (1 mark)
• if num_tickets >= 10 with correct syntax (1 mark)
• cost = cost - 50 inside the if (1 mark)
• print(cost) or printf("%d", cost); (1 mark)
(d) 12 × 40 = 480 (1 mark); 480 − 50 = 430, so 430 is printed (1 mark).
Next: Program Documentation: Internal and External Documentation
Syllabus link: This post is relevant to the CSEC® Information Technology Syllabus (CXC 30/G/SYLL 17), Section 8: Program Implementation, Specific Objectives 4 and 5 (“declare variables and constants using elementary data types” and “translate algorithmic statements into high-level language syntax”).
Attributions to media used in this post:
No third-party images are used in this post. Embedded videos are credited on YouTube to their channels.
Updates
2026/10/16
– Syntax highlighting added to code examples.
© 2026 Vedesh Kungebeharry. All rights reserved.