Computer Science Essentials, Unit 2, Lesson 3 of 6

Arrays and strings

GCSE Computer Science, Programming, 25 minutes

Sign in

One- and two-dimensional arrays, looping through them, and string handling: length, substrings, character codes and concatenation.

What you will learn

  • Read items from 1D and 2D arrays by index
  • Loop through an array
  • Use length, substring, ASC and CHR
  • Build a string from parts

Arrays

An array holds several values under one name.

Each item has an index, starting at 0.

scores = [12, 7, 19, 4]
print(scores[0])
print(scores[scores.length - 1])
scores ← [12, 7, 19, 4]
OUTPUT scores[0]
OUTPUT scores[LEN(scores) - 1]
scores = [12, 7, 19, 4]
print(scores[0])
print(scores[len(scores) - 1])

This prints 12, then 4: the last index is always the length minus 1. A loop from 0 to length - 1 visits every item.

A 2D array is a table: grid[row, column] in OCR style, grid[row][column] in AQA style and Python. The first index picks the row. Rows and columns are both numbered from 0.

For grid = [[5, 8, 1], [7, 2, 9]], row 0 is 5, 8, 1 and row 1 is 7, 2, 9, so grid[1, 2] is row 1, column 2: the value 9. A loop for c = 0 to 2 adding grid[0, c] adds up row 0: 5 + 8 + 1 = 14.

The rest of this lesson and its 7 practice questions are locked.

In this lesson

  1. Arrays
  2. String handling
  3. Try it: Read an array

7 practice questions, marked as you go; about 25 minutes in all.

Key terms

Array, Index, Length, ASCII

In this unit: Programming

Data types, operators, selection, loops, arrays, strings, subroutines, robust programs, SQL and files, read and traced step by step.

All of Programming