PROGRAMMING

Output


The first program that most programmers write in any language is a 'Hello world' program. It is a program that outputs some text to the screen.

OUTPUT "Hello world"


In many programming languages, the keyword for output is print. On modern computers, this means to display the specified information on the screen, not to print it on paper with a printer.

Input

Often, you will want the person using a program to type in some data that will be used in the program.

OUTPUT "Please enter your name"

name ← USERINPUT

OUTPUT "Your name is " + name




Variables

Variables are used to store pieces of data in your program. When you create a variable, an area of the computer’s memory is reserved for you, and the data is stored in it. You now control that area of memory: you can retrieve data from it, change the data in it, or get rid of the data altogether.

  • Assignment

  • Constants

  • Operators





Assignment

To create a variable, you must choose a variable name and make it equal to a value. This is known as assignment. For example:

age ← 18


In some languages, such as Java, you can name a variable and reserve the area in memory before providing a value, but in other languages, such as Python, you must specify the value of the variable when you create the variable.

In some languages, you also have to specify what type of data a variable will hold when you create the variable.


Assignment

A constant is a bit like a variable, except that a constant stores a value that cannot change during the execution of the program. The names of constants are usually written in capital letters. For example, you might store the current minimum wage per hour as a constant like this:

WAGE_RATE ← 8.21

OUTPUT "Enter the number of hours worked"

hours_worked ← USERINPUT

total_pay ← hours_worked * WAGE_RATE


If the value of the minimum wage changes, you only need to change the value once in the program — in the definition of the constant — in order to change the value everywhere.

Operator


An operator performs an operation on a variable or value. There are many types of operators. These include:

  • Arithmetic operators like plus (+) and divide (/), which you will already be familiar with from mathematics

  • The assignment operator, which is used to set a variable equal to a value







TASK 1

1 number ← 5

2 triple ← True

3

4 IF triple = True AND number ≥ 10 then

5 number ← number * 3

6 ELSE

7 number ← number + 5

8 ENDIF


What is the value of the variable number once the code has finished executing?

TASK 2

A banking system uses a constant to store the current rate of VAT so that it can be used in calculations.

VAT_RATE ← 0.20

Which is correct?

A - A constant is the same as a variable

B - The value of a constant can only change while the program is running

C - You cannot change the value of a constant in the program source code

D - A constant stores a value that does not change during the execution of the program