Presentation is loading. Please wait.

Presentation is loading. Please wait.

VARIABLES & CONSTANTS. Objective By the end of the lesson students should be able to:  Define a constant  Define a variable  Understand the rules for.

Similar presentations


Presentation on theme: "VARIABLES & CONSTANTS. Objective By the end of the lesson students should be able to:  Define a constant  Define a variable  Understand the rules for."— Presentation transcript:

1 VARIABLES & CONSTANTS

2 Objective By the end of the lesson students should be able to:  Define a constant  Define a variable  Understand the rules for naming variables and constants

3 FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES  In the computer, values are stored in memory locations.  There are many memory locations, so in order to keep track of where our values are stored we need to place a label or identifier on a particular memory location.  The label or identifier is called a variable.  A variable is a symbolic name assigned to a memory location that stores a particular value.

4 THE CONCEPT OF VARIABLES  The word variable is derived from the verb ‘to vary’. This means that the value stored in a particular location can change from time to time, although the label remains the same.  When new values are placed into previously assigned memory locations, the old values are replaced by the new.

5 Problem  E.g. A program is required to read three numbers then calculate and print the sum. INPUTPROCESSINGOUTPUT 3 numbers e.g. Num1, num2, num3 1.Read num 1, num2, num3 2.Calculate the sum 3.Print Sum Sum

6 Definition  Variable: Data that changes  A variable is a name associated with a particular memory location.  To refer to these locations we make up variable names. E.g. Num1, Num2, SUM  A variable can only hold one value at a time. When you give a variable a new value, the old one is lost.

7 FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES  When we say num1, num2, etc we are actually defining a variable or an identifier for each number, so that we can refer to (access) it later.  We need to tell the computer where to put the result of the addition of num1, num2, num3  Add num1 + num2 + num3 storing in Sum  This would tell the computer that the result of the computation should be stored in a memory location called Sum.

8 FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES Likewise, we must address a similar issue in the last two statements where we stated, Divide result by 3 Print result  Computations are performed by the ALU, part of the CPU and results are temporarily stored in registers within the CPU  These values must be stored in memory locations if they are to be accessed later

9 FINDING A SOLUTION TO THE PROBLEM THE CONCEPT OF VARIABLES We can illustrate the concept of a variable by using diagrams to show the memory locations: Memory locations 5 3 25 Num 1 Num 2 Num 3 Variables

10 THE CONCEPT OF VARIABLES CHOOSING VARIABLE NAMES  It’s good practice to choose variable names that reflect the kind of data that is being stored  It helps the programmer as well as the reader to understand the solution better

11 Try this example Write a program that allows the user to enter the radius of a circle. The program should then calculate the area of the circle and its circumference. (The area of a circle is pi*radius 2. The circumference of a circle is 2*pi*radius)

12 Solution INPUTPROCESSINGOUTPUT radiusRead radiusarea Calculate areacircumference Calculate circumference Print area, circumference NB. The user does not have to enter pi. The problem states that the user enters the radius. The value of pi is considered a constant i.e. the value will always remain the same. Pi will be used in the calculation of the circumference and the area of the circle.

13 Constant  Constant: Data that cannot be varied is a constant.  Can you think of other data that is constant.

14 Rules for Naming Variables  It must start with a letter  They can be any combination of characters, letters digits or underscore  It cannot exceed 31 characters  There should be no spaces in a variable name.  Pascal is case insensitive  There should be no use of reserved words e.g. var, begin, integer, if  A variable name should be meaningful. Netpay is a good variable name for representing net pay than X.  No special characters are allowed e.g. ;, @# ()+{} [] / $*=

15 Advantages of using variables over constants  The user of the program can vary the value by changing the input value when variables are used, whereas the user cannot change the value when constants are used.  There is no need to modify the program when changes of values are required if variables are used, whereas there is need to do so if constants are used.


Download ppt "VARIABLES & CONSTANTS. Objective By the end of the lesson students should be able to:  Define a constant  Define a variable  Understand the rules for."

Similar presentations


Ads by Google