What is a Program in Computer?
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 language. The computer can only do exactly what the program tells the computer to do. The 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 a hard disk.
A few examples of programs are:
- A program to find the average marks of a student in the final exam.
- A program is to find the area of a circle for a given radius.
- A program to solve a quadratic equation.
- A program to control the financial matters of a company.
- A program to manage items in a grocery shop.
What is a Programming Language?
Natural languages, like Urdu, English, Arabic, French, etc. are the means of communication among people to share their ideas and views. In the same way, programming languages are the mean of communication between users and computers. Users, as well as computers, understand these languages.
A programming language is a 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, BASIC, etc.
Constants and Variables in Programming Language
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 sequences 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 a sequence of characters or strings enclosed in double quotations. String variables can store string constants. For example:
In BASIC Programming Language $ (Dollar Sign) is used as the last character with a 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 results 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 a wrong answer due to the programmer’s own logical mistake. These errors occur due to the wrong use of formulae or providing the 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 accidentally 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 Language
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 that represents a numeric value is called an Arithmetic Expression. An Arithmetic Expression is evaluated by performing a sequence of arithmetic operations to obtain a numeric value.
Some examples of Arithmetic Expressions are as follows:
An algebraic expression cannot be used directly in programming. It must be converted into computer-understandable expression. A 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 (<,>,<>,<=,>=)
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 in 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|
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.
These are used to perform comparisons on two values. The result of the 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|
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|