← IGCSE Computer Science Paper 2

Length check

A length check tests the number of characters in an input is the required length or within a limit, using LENGTH, e.g. a password of at least 8 characters or a PIN of exactly 4.

Understanding Length check in IGCSE Computer Science Paper 2

What it does: <= 12 accepts lengths of 12 or fewer and rejects anything longer, which is a maximum length check. AND means both conditions must be true, so the length must be between 6 and 12 inclusive. A length check tests the number of characters in the input. A range check tests the size of a number. Exactly 4 needs =. Using > 4 would reject a correct 4-digit PIN. A maximum length uses <=. Using >= 12 would only accept names of 12 or more characters. Validation checks only that data is reasonable. A wrong 4-digit PIN still passes a length check.

Common mistakes: A password is text, so it must be declared as STRING. LENGTH counts the characters in a string, so Password cannot be an INTEGER. The line should be DECLARE Password : STRING. A length check must test the number of characters, not the value typed. It should be IF LENGTH(PIN) = 4 THEN, so a PIN such as 1234 passes because it has exactly 4 characters.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 47–57) and word search (Modules 47–70).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which check makes sure a password has at least 8 characters?
Length checkRange check
Matches
IF Length(PIN) = 4 THEN
↓
The function name is written in the wrong case. Keywords and functions must be in capital letters, so it should be LENGTH(PIN).
Applying
Meanings
Term
LENGTH(Password) >= 8
Meaning
A length check tests the number of characters in an input is the required length or within a limit, using LENGTH, e.g. a password of at least 8 characters or a PIN of exactly 4.
Practice
OUTPUT "Enter a password"
Testing
Gaps
IF (Password) >= 8 THEN
Check Answer
Spots
1DECLARE Password : INTEGER
2INPUT Password

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Abyssal Dragon' badge — Deep Sea Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which check makes sure a password has at least 8 characters?
A. Length check ✓
B. Range check
A length check tests the number of characters in the input. A range check tests the size of a number.
2. Which condition is a valid length check for a password of at least 8 characters?
A. LENGTH(Password) >= 8 ✓
B. Password >= 8
LENGTH gives the number of characters. Password >= 8 compares the text itself with 8, which does not check how long it is.
3. A four-digit PIN must have exactly 4 characters. Which condition accepts only valid PINs?
A. LENGTH(PIN) > 4
B. LENGTH(PIN) = 4 ✓
Exactly 4 needs =. Using > 4 would reject a correct 4-digit PIN.
4. A user enters the PIN 123. Does it pass the length check LENGTH(PIN) = 4?
A. No, it has only 3 characters ✓
B. Yes, because the digits are all numbers
A length check only counts characters. 123 has 3 characters, so it fails. It does not matter that they are digits.
5. A username must be no more than 12 characters long. Which condition is correct?
A. LENGTH(Username) >= 12
B. LENGTH(Username) <= 12 ✓
A maximum length uses <=. Using >= 12 would only accept names of 12 or more characters.

Match the Pairs

LENGTH(Password) >= 8 → Checks that the password has at least 8 characters (a minimum length check)
LENGTH(PIN) = 4 → Checks that the PIN has exactly 4 characters (an exact length check)
A length check can set a maximum length, so a username must not be longer than 12 characters → LENGTH(Username) <= 12
A length check can set both a minimum and a maximum, so a username must be 6 to 12 characters → LENGTH(Username) >= 6 AND LENGTH(Username) <= 12
IF Length(PIN) = 4 THEN → The function name is written in the wrong case. Keywords and functions must be in capital letters, so it should be LENGTH(PIN).
IF LENGTH(Password) = 8 THEN → = tests for exactly 8 characters, so longer passwords are wrongly rejected. A minimum length needs >= 8.

Key Term

LENGTH(Password) >= 8
A length check tests the number of characters in an input is the required length or within a limit, using LENGTH, e.g. a password of at least 8 characters or a PIN of exactly 4.

Fill in the Blanks

1. IF LENGTH(Password) >= 8 THEN
Hint: Type the function that counts the characters in a string.
2. IF LENGTH(PIN) = 4 THEN
Hint: Type the relational operator that checks for an exact length.
3. IF LENGTH(Password) >= 8 THEN
Hint: Type the operator that means at least the minimum length.
4. IF LENGTH(Username) <= 12 THEN
Hint: Type the operator that keeps the length within a maximum.
5. REPEAT INPUT PIN UNTIL LENGTH(PIN) = 4
Hint: Type the exact number of digits a PIN must have.

Spot the Error

Question 1
A DECLARE Password : INTEGER
B INPUT Password
C IF LENGTH(Password) >= 8 THEN
D OUTPUT "Accepted"
E ENDIF
Line A — A password is text, so it must be declared as STRING. LENGTH counts the characters in a string, so Password cannot be an INTEGER. The line should be DECLARE Password : STRING.
Question 2
A INPUT PIN
B IF PIN = 4 THEN
C OUTPUT "PIN OK"
D ELSE
E OUTPUT "Must be 4 digits"
F ENDIF
Line B — A length check must test the number of characters, not the value typed. It should be IF LENGTH(PIN) = 4 THEN, so a PIN such as 1234 passes because it has exactly 4 characters.
Question 3
A INPUT Password
B WHILE LENGTH(Password) < 8 DO
C OUTPUT Too short
D INPUT Password
E ENDWHILE
Line C — Text in an OUTPUT statement must be inside double quotes. The line should be OUTPUT "Too short".
Question 4
A INPUT Username
B IF LENGTH(Username) > 12 THEN
C PRINT "Too long"
D ELSE
E OUTPUT "Saved"
F ENDIF
Line C — Cambridge pseudocode uses OUTPUT to display a message, not PRINT. The line should be OUTPUT "Too long".
Question 5
A DECLARE PIN STRING
B INPUT PIN
C IF LENGTH(PIN) = 4 THEN
D OUTPUT "Accepted"
E ENDIF
Line A — A DECLARE statement needs a colon between the identifier and the data type. The line should be DECLARE PIN : STRING.

Student with a class code? Join your class →