Basic 8, Programming
Basic 8, Programming
THINKING
Introduction to Programming
WHAT IS PROGRAMMING?
Constants : Is define as values that are used within a program and do not change.
Variables : They are spaces that are reserved for data to be entered or can also be define as a
value within a program whose value can change during running of a program.
Input : it refers to information that is accepted from the user or other sources into a program.
NB: the input is not what the programmer enters but the user of the program. Example, if you
want to buy airtime from MTN, after dialing your mobile money short code, a menu then
displays and you are ask to enter an option, the option is the input your are doing.
VARIABLE AND
CONSTANTS
Data : data is input into a program variable.
A variable is a named of piece of memory whose value can change during the
running of the program.
Example 1: A program to calculate the average of three numbers.
Write TotalCost.
Can you identify the variable and constant in this program?
Variable used are Songs and TotalCost
The constant is : 5.99
NAMING OF IDENTIFIERS
Identifiers: They are name of things such as variable and constant that appears in a program.
The following are basic rules for naming variables: NB: the rules are different for all
programming language, but these are generally accepted:
1. Identify name consist of letters, digits, the underscore character ( _ ), and the Dollar
sign( $ ) and MUST begin with a letter, underscore or the dollar sign.
2. Identifiers can be made of only letters, digits , the underscore and the dollar sign. No
other symbol is permitted as an identifier.
EXAMPLE:
1. first, NUMBER, Conversion, NaMe…………………..
2. payRate, ComputeAverage, Firstname, Lastname………..
3. Counter1, number1, num2, f2name.
4. $Amount, _amount, _Amount, _2place,
5. first_name,
OPERATIONS ON DATA
• Programming languages support some form of arithmetic operations
• Some arithmetic operations includes, addition, subtraction,
multiplication, division, exponentiation and modulus.
• Operator Computer symbol Example
Addition + 3 + 8 = 11
Subtraction - 9–4=5
Multiplication * 8 * 3 = 24
Division / 24 / 4 = 6
Exponentiation ^ 2^3 = 8
Modulus % 14 % 4 = 2
Hierarchy of Operations/ Operator
Precedence
• First: perform operations inside parenthesis ( from inside out if more
than one )
• Second: perform exponentiation
• Third: Do multiplications, divisions, and modulus from left to right. (if
there are more than one)
• Fourth: do additions and subtractions from left to right (if there are
more than one)
Example of hierarchy of Operations
3 * ( 6 + 2 )/ 12 + ( 7 – 5 )^ 2 * 3 = ?
( ) first : = 3* 8 / 12 + 2 ^ 2 * 3
^ next : = 3 * 8 / 12 + 4 * 3
Leftmost * next : = 24 /12 + 4 * 3
Division next: = 2 + 4 * 3
Multiply next: 2 + 12
Addition last = 14.