This topic covers the building blocks of a program: the data it holds, the constructs that control it, the subroutines it is divided into, and the checks that make it robust.
A data type decides what values can be stored and what can be done with them.
Changing a value from one type to another is casting. Input read from a keyboard arrives as a string, so it must be cast before it can be used in arithmetic.
A variable is a named store whose value can change while the program runs. A constant is a named value that cannot.
Giving a value a name once, and using the name everywhere else, means a change has to be made in only one place.
Sequence means the statements run one after another in the order they are written. Changing that order changes what the program does.
Selection chooses between paths. An if statement runs its block when a condition is true; else covers the case where it is false.
Conditions evaluate to true or false, so they are Boolean expressions.
Iteration repeats a block of statements.
Addition, subtraction, multiplication and division work as expected.
Integer division gives the whole number of times one value divides into another, and modulus gives the remainder left over. Exponentiation raises a value to a power.
Relational operators compare two values and give a Boolean result: equal to, not equal to, less than, greater than, less than or equal to, and greater than or equal to.
AND is true only when both conditions are true. OR is true when at least one is. NOT reverses a condition.
These can be combined, and brackets decide the order they are applied in.
An array holds several values of the same type under one identifier, reached by an index. In most languages the first index is 0.
A two-dimensional array is indexed by row and column, which suits grids and tables.
A record groups related fields of different types about one item, such as a name, an age and a membership number. An array of records stores many such items.
An input statement obtains a value from the user. An output statement displays a value. Prompting clearly before an input tells the user what is expected.
Common string operations are finding the length, taking a substring, finding the position of a character, joining strings by concatenation, and converting case.
Strings can also be converted to and from integers and reals.
A random number generator produces a value that cannot be predicted, within a range the program sets. It is used for games, sampling and simulations.
A subroutine is a named block of code that carries out one task and can be called from anywhere. Subroutines remove repetition and let each part be tested separately.
A function returns a value to whatever called it. A procedure carries out a task without returning a value.
A parameter is the name a subroutine uses for a value passed to it. The value supplied at the call is the argument.
A function sends a value back with a return statement.
A local variable exists only inside the subroutine that declares it, so the same name can be reused elsewhere without clashing.
A global variable is available throughout the program. Globals make a program harder to follow, because any part of it can change the value.
A program can open a text file, read from it or write to it, and close it. Data written line by line, with values separated by commas, is a CSV file.
Meaningful identifiers, consistent indentation, comments and white space make a program easier to follow and to change later. None of them affect what the program does.
Validation checks that entered data follows the rules. A range check, a length check, a presence check and a format check are the common ones.
Authentication establishes who the user is, most simply by checking a username against a stored password.
Iterative testing happens during development, on each part as it is written. Final testing checks the finished program against its original requirements.
Tests should use normal data that must be accepted, boundary data at the edge of the allowed range, and erroneous data that must be rejected.
Defensive design means anticipating how a program might be misused and handling it. It includes validating input, dealing with errors without crashing, and keeping code maintainable.
Back to Programming practice ยท View all Programming questions