Data Types and Structures

Variable

A variable is a storage location used to hold a value required by a program. Each variable has a unique name. The data value held in a variable may change when a program is run

 

Data Types

The type of data that will be sored in a variable must also be defined.

Arrays

An array is an example of a data structure.  Often a program will have to store lots of information at the one time.​

Arrays allow the programmer to store more than one piece of data within a single data structure.​  Data that is stored in an array must be the same data type, an array of integers, an of strings etc​.  Arrays must be given suitable identifiers just like a variable

Each piece of information stored in an array needs to be given a number so that it can be uniquely identified.  The number is called an “index”.  The index always starts at zero.

Creating an Array

Subjectlist= [“Maths”,”English”,”French”,”Computing”]

Displaying an array

A For Loop is an arrays best friend for one reason; the counter!

As we should remember each For Loop has a counter and every time the loop is completed the counter increases by one. This is extremely useful when  displaying information from arrays.

Populating an array

This allows the program to ask the user a question multiple times and saving the inputs into different indexes of the array. As the counter increases by one each time, it is perfect to use as a reference for the index.

 

Array with a Running Total

The example that is shown below is a program that uses a loop to access each element of an array to calculate the total value within the array.