A computer program is a sequence of instructions given to the computer to perform a specific task. Programs are written in languages called computer languages or programming languages. Computer can only do exact what the program tells the computer to do. Computer follows the instructions written in the program to perform a particular task. A computer program is stored as a file on the computer’s storage medium, like hard disk.
Few examples of programs are:
- A program to find the average marks of a student in the final exam.
- A program is to find area of a circle for given radius.
- A program to solve a quadratic equation.
- A program to control financial matters of a company.
- A program to manage items in a grocery shop.
Natural languages, like Urdu, English, Arabic and French, etc. are the means of communication among people to share their ideas and views. In the same ways, programming languages are the mean of communication between users and the computers. Users as well as computers understand these languages.
A programming language is language designed to develop programs or instructions to communicate with the computer to solve various problems. There are different programming languages having their own set of rules and regulations that the computer programs follow to write computer programs. Most of these languages use English words as statements and commands to create programs. Some examples of programming languages are FORTRAN, COBOL, C, C++, C#, JAVA and BASIC, etc.
Constants and Variables in Programming
Constants and Variables are the two main important terminologies used in programming.
These are the quantities whose values cannot be changed during program execution or running. These are the named space in the computer’s memory. Constants are classified as string constants and numeric constants.
- String Constants
These are sequence of alphabetic or alphanumeric characters enclosed in double quotation marks. For example, “Male”, “Married”, Hairs”, “Pakistan” and “H.No.107” etc.
- Numeric Constants
These are the numbers, for example 117, 20.50, -50 etc.
It is a named space in the computer’s memory whose value can be changed during the execution of a program. Variables give meaningful names to constants. They are also used to store data and results during program execution. There are two types of variables.
- Numeric Variables
These are used to store numeric constants such as 10, 134, 12.34 and -312, etc. For example:
X=25 (X is a variable and 25 is a numeric constant)
NUM=35.6 (NUM is a variable and 35.6 is a numeric constant)
- String Variables
These are used to store sequence of characters or string enclosed in double quotations. String variables can store string constants. For example:
In BASIC Programming Language $ (Dollar Sign) is used as a last character with string variable.
In programming languages, like BASIC, certain rules are followed to declare/define a variable name. These are:
- Alphabets and numbers can be used for variables.
- The first character of the variable should be an alphabet.
- No special symbol is allowed except Underscore ( _ ).
- Underscore ( _ ) cannot be used as a first or last character.
Syntax and Logical Errors
In a program, the occurrence of incorrect or unexpected result is called an error. Errors occur due to some mistake in performing operations in a program. These are two common types of errors in programming.
- Syntax Error
It occurs when the instructions written in a program do not follow the rules of the programming language. Syntax Errors are easy to find and correct because the computer finds them for the user.
Example: PRNT instead of PRINT
5=X instead of X=5
- Logical Error
A logical error is an error resulting in wrong answer due to programmers own logical mistake. These errors occur due to wrong use of formulae or providing wrong value to a variable. If a programmer writes a statement that is logically incorrect, the computer will understand and execute it but the result will be wrong. For example, if a programmer accidently multiplies two variables when he or she meant to divide them, the program will give an incorrect result, but no error message. Such errors cannot be detected by a computer therefore they are hard to find and correct.
Examples: SUM=X-Y instead of SUM=X+Y
AREA=Pie* instead of AREA=Pie*
Arithmetic Expression in Programming
An expression is a combination of symbols and operators that represent a value. Every expression consists of at least one operand and can have one or more operators. Operands are values, whereas operators are symbols that represent particular actions. For example, in the expression
X + 3
X and 3 are operands, and + is an operator.
An expression which represents a numeric value is called an Arithmetic Expression. An Arithmetic Expression is evaluated by performing a sequence of arithmetic operations to obtain numeric value.
Some examples of Arithmetic Expressions are follows:
Algebraic expression cannot be used directly in programming. It must be converted into computer understandable expression. Few examples are given in the following table:
|V=4/3 pie R^3||V=4/3 *Pie*R^3|
Operators in Programming
These are symbols that indicate the type of operation to be performed on the data. There are three common types of operators which are given below:
Arithmetic Operator (+,-,*,/,^.MOD)
Assignment Operator (=)
Relational Operator (<,>,<>,<=,>=)
· Arithmetic Operators
These are used to perform mathematical calculations like addition, subtraction, division, multiplication and exponentiation. The following table gives the description of arithmetic operators with examples.
|+||Addition||Gives the sum of values||A+B+C, X+50, 67+45|
|–||Subtraction||Gives the difference of values||X-56, A-C, 80-56|
|*||Multiplication||Gives the product of values||A*B, Y*50, 45*60|
|^||Exponentiation||Raise the value of the power of an exponent||A^10, 5^2, A^B|
|/||Division||Gives the quotient||X/Y, B/40, 125/25|
|MOD||Modulus||Gives the remainder after division||35 MOD 6, X MOD Y|
· Assignment Operator
An operator which is used to assign a value to a variable is called Assignment Operator. In programming languages “Equal sign” (=) is used as an assignment operator.
In this example, 5 is assigned to the variable A.
· Relational Operators
These are used to perform comparisons on two values. The result of comparison is either true (non zero) or false (zero). The following table gives the description of relational operators with examples:
|=||Equal to||Returns true if the two values are equal, and false if not.||A=B|
|<>||Not equal to||Returns true if the two values are not equal, and false if they are equal||5<>7|
|>||Greater than||Returns true if the first number is greater than the second, and false if not.||15>11|
|<||Less than||Returns true if the first no is smaller than the second, and false if not||7<9|
|>=||Greater than |
or Equal to
|Returns true if the first no is greater than or equal to the second, and false if not||(X+1)>=7|
Or Equal to
|Returns true if the first no is less than or equal to the second, and false if not||Y<=5|